KiCad PCB EDA Suite
sg_appearance.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2017 Cirilo Bernardo <cirilo.bernardo@gmail.com>
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <iostream>
26 #include <utility>
27 #include <sstream>
28 #include <wx/log.h>
29 
31 #include "3d_cache/sg/sg_helpers.h"
32 
33 
34 SGAPPEARANCE::SGAPPEARANCE( SGNODE* aParent ) : SGNODE( aParent )
35 {
37 
38  // defaults in accord with VRML2.0 spec
39  ambient.SetColor( 0.05317f, 0.17879f, 0.01804f );
40  shininess = 0.2f;
41  transparency = 0.0f;
42  diffuse.SetColor( 0.8f, 0.8f, 0.8f );
43 
44  if( nullptr != aParent && S3D::SGTYPE_SHAPE != aParent->GetNodeType() )
45  {
46  m_Parent = nullptr;
47 
48  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [BUG] inappropriate parent to SGAPPEARANCE (type %s )",
49  __FILE__, __FUNCTION__, __LINE__, aParent->GetNodeType() );
50  }
51  else if( nullptr != aParent && S3D::SGTYPE_SHAPE == aParent->GetNodeType() )
52  {
53  m_Parent->AddChildNode( this );
54  }
55 }
56 
57 
59 {
60 }
61 
62 
63 bool SGAPPEARANCE::SetParent( SGNODE* aParent, bool notify )
64 {
65  if( nullptr != m_Parent )
66  {
67  if( aParent == m_Parent )
68  return true;
69 
70  // handle the change in parents
71  if( notify )
72  m_Parent->unlinkChildNode( this );
73 
74  m_Parent = nullptr;
75 
76  if( nullptr == aParent )
77  return true;
78  }
79 
80  // only a SGSHAPE may be parent to a SGAPPEARANCE
81  if( nullptr != aParent && S3D::SGTYPE_SHAPE != aParent->GetNodeType() )
82  return false;
83 
84  m_Parent = aParent;
85 
86  if( m_Parent )
87  m_Parent->AddChildNode( this );
88 
89  return true;
90 }
91 
92 
93 bool SGAPPEARANCE::SetEmissive( float aRVal, float aGVal, float aBVal )
94 {
95  return emissive.SetColor( aRVal, aGVal, aBVal );
96 }
97 
98 
99 bool SGAPPEARANCE::SetEmissive( const SGCOLOR* aRGBColor )
100 {
101  wxCHECK_MSG( aRGBColor, false, "NULL pointer passed for aRGBColor" );
102 
103  return emissive.SetColor( aRGBColor );
104 }
105 
106 
107 bool SGAPPEARANCE::SetEmissive( const SGCOLOR& aRGBColor )
108 {
109  return emissive.SetColor( aRGBColor );
110 }
111 
112 
113 bool SGAPPEARANCE::SetDiffuse( float aRVal, float aGVal, float aBVal )
114 {
115  return diffuse.SetColor( aRVal, aGVal, aBVal );
116 }
117 
118 
119 bool SGAPPEARANCE::SetDiffuse( const SGCOLOR* aRGBColor )
120 {
121  wxCHECK_MSG( aRGBColor, false, "NULL pointer passed for aRGBColor" );
122 
123  return diffuse.SetColor( aRGBColor );
124 }
125 
126 
127 bool SGAPPEARANCE::SetDiffuse( const SGCOLOR& aRGBColor )
128 {
129  return diffuse.SetColor( aRGBColor );
130 }
131 
132 
133 bool SGAPPEARANCE::SetSpecular( float aRVal, float aGVal, float aBVal )
134 {
135  return specular.SetColor( aRVal, aGVal, aBVal );
136 }
137 
138 
139 bool SGAPPEARANCE::SetSpecular( const SGCOLOR* aRGBColor )
140 {
141  wxCHECK_MSG( aRGBColor, false, "NULL pointer passed for aRGBColor" );
142 
143  return specular.SetColor( aRGBColor );
144 }
145 
146 bool SGAPPEARANCE::SetSpecular( const SGCOLOR& aRGBColor )
147 {
148  return specular.SetColor( aRGBColor );
149 }
150 
151 
152 bool SGAPPEARANCE::SetAmbient( float aRVal, float aGVal, float aBVal )
153 {
154  return ambient.SetColor( aRVal, aGVal, aBVal );
155 }
156 
157 
158 bool SGAPPEARANCE::SetAmbient( const SGCOLOR* aRGBColor )
159 {
160  wxCHECK_MSG( aRGBColor, false, "NULL pointer passed for aRGBColor" );
161 
162  return ambient.SetColor( aRGBColor );
163 }
164 
165 
166 bool SGAPPEARANCE::SetAmbient( const SGCOLOR& aRGBColor )
167 {
168  return ambient.SetColor( aRGBColor );
169 }
170 
171 
172 SGNODE* SGAPPEARANCE::FindNode( const char* aNodeName, const SGNODE* aCaller) noexcept
173 {
174  if( nullptr == aNodeName || 0 == aNodeName[0] )
175  return nullptr;
176 
177  if( !m_Name.compare( aNodeName ) )
178  return this;
179 
180  return nullptr;
181 }
182 
183 
184 void SGAPPEARANCE::unlinkChildNode( const SGNODE* aCaller ) noexcept
185 {
186  wxCHECK_MSG( aCaller, /* void */,
187  "unexpected code branch; node should have no children or refs" );
188 }
189 
190 
191 void SGAPPEARANCE::unlinkRefNode( const SGNODE* aCaller ) noexcept
192 {
193  wxCHECK_MSG( aCaller, /* void */,
194  "unexpected code branch; node should have no children or refs" );
195 }
196 
197 
198 bool SGAPPEARANCE::AddRefNode( SGNODE* aNode ) noexcept
199 {
200  wxCHECK_MSG( aNode, false, "this node does not accept children or refs" );
201 
202  // This is redundant but it keeps gcc from generating a warning on debug builds.
203  return false;
204 }
205 
206 
207 bool SGAPPEARANCE::AddChildNode( SGNODE* aNode ) noexcept
208 {
209  wxCHECK_MSG( aNode, false, "this node does not accept children or refs" );
210 
211  // This is redundant but it keeps gcc from generating a warning on debug builds.
212  return false;
213 }
214 
215 
217 {
218  m_written = false;
219 
220  // rename this node
221  m_Name.clear();
222  GetName();
223 }
224 
225 
226 bool SGAPPEARANCE::WriteVRML( std::ostream& aFile, bool aReuseFlag )
227 {
228  if( aReuseFlag )
229  {
230  if( !m_written )
231  {
232  aFile << " appearance DEF " << GetName() << " Appearance {\n";
233  m_written = true;
234  }
235  else
236  {
237  aFile << " appearance USE " << GetName() << "\n";
238  return true;
239  }
240  }
241  else
242  {
243  aFile << " appearance Appearance {\n";
244  }
245 
246  aFile << " material Material {\n";
247 
248  std::string tmp;
249  float ambr, ambg, ambb;
250  ambient.GetColor( ambr, ambg, ambb );
251  float amb = ( 0.212671 * ambr + 0.71516 * ambg + 0.072169 * ambb );
252  diffuse.GetColor( ambr, ambg, ambb );
253  float den = ( 0.212671 * ambr + 0.71516 * ambg + 0.072169 * ambb );
254 
255  if( den < 0.004f )
256  den = 0.004f;
257 
258  amb /= den;
259 
260  if( amb > 1.0f )
261  amb = 1.0f;
262 
263  S3D::FormatFloat( tmp, amb );
264  aFile << " ambientIntensity " << tmp << "\n";
265 
266  float red, green, blue;
267  diffuse.GetColor( red, green, blue );
268  S3D::FormatFloat( tmp, red );
269  aFile << " diffuseColor " << tmp << " ";
270  S3D::FormatFloat( tmp, green );
271  aFile << tmp << " ";
272  S3D::FormatFloat( tmp, blue);
273  aFile << tmp << "\n";
274 
275  emissive.GetColor( red, green, blue );
276  S3D::FormatFloat( tmp, red );
277  aFile << " emissiveColor " << tmp << " ";
278  S3D::FormatFloat( tmp, green );
279  aFile << tmp << " ";
280  S3D::FormatFloat( tmp, blue);
281  aFile << tmp << "\n";
282 
283  S3D::FormatFloat( tmp, shininess );
284  aFile << " shininess " << tmp << "\n";
285 
286  specular.GetColor( red, green, blue );
287  S3D::FormatFloat( tmp, red );
288  aFile << " specularColor " << tmp << " ";
289  S3D::FormatFloat( tmp, green );
290  aFile << tmp << " ";
291  S3D::FormatFloat( tmp, blue);
292  aFile << tmp << "\n";
293 
295  aFile << " transparency " << tmp << "\n";
296 
297  aFile << "} }\n";
298 
299  return true;
300 }
301 
302 
303 bool SGAPPEARANCE::WriteCache( std::ostream& aFile, SGNODE* parentNode )
304 {
305  if( nullptr == parentNode )
306  {
307  wxCHECK_MSG( m_Parent, false, "corrupt data; m_aParent is NULL" );
308 
309  SGNODE* np = m_Parent;
310 
311  while( nullptr != np->GetParent() )
312  np = np->GetParent();
313 
314  if( np->WriteCache( aFile, nullptr ) )
315  {
316  m_written = true;
317  return true;
318  }
319 
320  return false;
321  }
322 
323  wxCHECK_MSG( parentNode == m_Parent, false, "corrupt data; parentNode != m_aParent" );
324 
325  if( !aFile.good() )
326  {
327  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [INFO] bad stream", __FILE__, __FUNCTION__, __LINE__ );
328 
329  return false;
330  }
331 
332  aFile << "[" << GetName() << "]";
333  S3D::WriteColor( aFile, ambient );
334  aFile.write( (char*) &shininess, sizeof( shininess ) );
335  aFile.write( (char*) &transparency, sizeof( transparency ) );
336  S3D::WriteColor( aFile, diffuse );
337  S3D::WriteColor( aFile, emissive );
338  S3D::WriteColor( aFile, specular );
339 
340  if( aFile.fail() )
341  return false;
342 
343  m_written = true;
344  return true;
345 }
346 
347 
348 bool SGAPPEARANCE::ReadCache( std::istream& aFile, SGNODE* parentNode )
349 {
350  S3D::ReadColor( aFile, ambient );
351  aFile.read( (char*) &shininess, sizeof( shininess ) );
352  aFile.read( (char*) &transparency, sizeof( transparency ) );
353  S3D::ReadColor( aFile, diffuse );
354  S3D::ReadColor( aFile, emissive );
355  S3D::ReadColor( aFile, specular );
356 
357  if( aFile.fail() )
358  return false;
359 
360  return true;
361 }
SGCOLOR diffuse
Definition: sg_appearance.h:77
SGAPPEARANCE(SGNODE *aParent)
bool ReadCache(std::istream &aFile, SGNODE *parentNode) override
Reads binary format data from a cache file.
SGNODE * m_Parent
Pointer to parent node; may be NULL for top level transform.
Definition: sg_node.h:227
bool ReadColor(std::istream &aFile, SGCOLOR &aColor)
Definition: sg_helpers.cpp:294
bool AddRefNode(SGNODE *aNode) noexcept override
virtual ~SGAPPEARANCE()
bool SetColor(float aRedVal, float aGreenVal, float aBlueVal)
Definition: sg_base.cpp:85
virtual bool SetParent(SGNODE *aParent, bool notify=true) override
Set the parent SGNODE of this object.
bool WriteColor(std::ostream &aFile, const SGCOLOR &aColor)
Definition: sg_helpers.cpp:180
The base class of all Scene Graph nodes.
Definition: sg_node.h:74
SGCOLOR emissive
Definition: sg_appearance.h:78
void GetColor(float &aRedVal, float &aGreenVal, float &aBlueVal) const noexcept
Definition: sg_base.cpp:59
void ReNameNodes(void) override
Rename a node and all its child nodes in preparation for write operations.
void unlinkChildNode(const SGNODE *aNode) noexcept override
Remove references to an owned child.
void FormatFloat(std::string &result, double value)
Definition: sg_helpers.cpp:37
SGNODE * FindNode(const char *aNodeName, const SGNODE *aCaller) noexcept override
Search the tree of linked nodes and return a reference to the first node found with the given name.
const char * GetName(void)
Definition: sg_node.cpp:146
SGCOLOR ambient
Definition: sg_appearance.h:76
bool m_written
Set to true when the object has been written after a ReNameNodes().
Definition: sg_node.h:230
virtual void unlinkChildNode(const SGNODE *aNode)=0
Remove references to an owned child.
float transparency
Definition: sg_appearance.h:75
SGNODE * GetParent(void) const noexcept
Returns a pointer to the parent SGNODE of this object or NULL if the object has no parent (ie.
Definition: sg_node.cpp:110
std::string m_Name
name to use for referencing the entity by name.
Definition: sg_node.h:229
virtual bool WriteCache(std::ostream &aFile, SGNODE *parentNode)=0
Write this node's data to a binary cache file.
bool WriteVRML(std::ostream &aFile, bool aReuseFlag) override
Writes this node's data to a VRML file.
bool SetSpecular(float aRVal, float aGVal, float aBVal)
S3D::SGTYPES m_SGtype
Type of Scene Graph node.
Definition: sg_node.h:228
bool SetEmissive(float aRVal, float aGVal, float aBVal)
bool AddChildNode(SGNODE *aNode) noexcept override
SGCOLOR specular
Definition: sg_appearance.h:79
virtual bool AddChildNode(SGNODE *aNode)=0
bool SetAmbient(float aRVal, float aGVal, float aBVal)
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...
void unlinkRefNode(const SGNODE *aNode) noexcept override
Remove pointers to a referenced node.
bool WriteCache(std::ostream &aFile, SGNODE *parentNode) override
Write this node's data to a binary cache file.
bool SetDiffuse(float aRVal, float aGVal, float aBVal)
S3D::SGTYPES GetNodeType(void) const noexcept
Return the type of this node instance.
Definition: sg_node.cpp:104