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  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <iostream>
25 #include <utility>
26 #include <sstream>
27 #include <wx/log.h>
28 
30 #include "3d_cache/sg/sg_helpers.h"
31 
32 SGAPPEARANCE::SGAPPEARANCE( SGNODE* aParent ) : SGNODE( aParent)
33 {
35 
36  // defaults in accord with VRML2.0 spec
37  ambient.SetColor( 0.05317, 0.17879, 0.01804 );
38  shininess = 0.2;
39  transparency = 0.0;
40  diffuse.SetColor( 0.8, 0.8, 0.8 );
41 
42  if( NULL != aParent && S3D::SGTYPE_SHAPE != aParent->GetNodeType() )
43  {
44  m_Parent = NULL;
45 
46 #ifdef DEBUG
47  std::ostringstream ostr;
48  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
49  ostr << " * [BUG] inappropriate parent to SGAPPEARANCE (type ";
50  ostr << aParent->GetNodeType() << ")";
51  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
52 #endif
53  }
54  else if( NULL != aParent && S3D::SGTYPE_SHAPE == aParent->GetNodeType() )
55  {
56  m_Parent->AddChildNode( this );
57  }
58 
59  return;
60 }
61 
62 
64 {
65  return;
66 }
67 
68 
69 bool SGAPPEARANCE::SetParent( SGNODE* aParent, bool notify )
70 {
71  if( NULL != m_Parent )
72  {
73  if( aParent == m_Parent )
74  return true;
75 
76  // handle the change in parents
77  if( notify )
78  m_Parent->unlinkChildNode( this );
79 
80  m_Parent = NULL;
81 
82  if( NULL == aParent )
83  return true;
84  }
85 
86  // only a SGSHAPE may be parent to a SGAPPEARANCE
87  if( NULL != aParent && S3D::SGTYPE_SHAPE != aParent->GetNodeType() )
88  return false;
89 
90  m_Parent = aParent;
91 
92  if( m_Parent )
93  m_Parent->AddChildNode( this );
94 
95  return true;
96 }
97 
98 
99 bool SGAPPEARANCE::SetEmissive( float aRVal, float aGVal, float aBVal )
100 {
101  return emissive.SetColor( aRVal, aGVal, aBVal );
102 }
103 
104 
105 bool SGAPPEARANCE::SetEmissive( const SGCOLOR* aRGBColor )
106 {
107  if( NULL == aRGBColor )
108  {
109  #ifdef DEBUG
110  std::ostringstream ostr;
111  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
112  ostr << " * [BUG] NULL pointer passed for aRGBColor";
113  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
114  #endif
115 
116  return false;
117  }
118 
119  return emissive.SetColor( aRGBColor );
120 }
121 
122 
123 bool SGAPPEARANCE::SetEmissive( const SGCOLOR& aRGBColor )
124 {
125  return emissive.SetColor( aRGBColor );
126 }
127 
128 
129 bool SGAPPEARANCE::SetDiffuse( float aRVal, float aGVal, float aBVal )
130 {
131  return diffuse.SetColor( aRVal, aGVal, aBVal );
132 }
133 
134 
135 bool SGAPPEARANCE::SetDiffuse( const SGCOLOR* aRGBColor )
136 {
137  if( NULL == aRGBColor )
138  {
139  #ifdef DEBUG
140  std::ostringstream ostr;
141  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
142  ostr << " * [BUG] NULL pointer passed for aRGBColor";
143  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
144  #endif
145 
146  return false;
147  }
148 
149  return diffuse.SetColor( aRGBColor );
150 }
151 
152 
153 bool SGAPPEARANCE::SetDiffuse( const SGCOLOR& aRGBColor )
154 {
155  return diffuse.SetColor( aRGBColor );
156 }
157 
158 
159 bool SGAPPEARANCE::SetSpecular( float aRVal, float aGVal, float aBVal )
160 {
161  return specular.SetColor( aRVal, aGVal, aBVal );
162 }
163 
164 
165 bool SGAPPEARANCE::SetSpecular( const SGCOLOR* aRGBColor )
166 {
167  if( NULL == aRGBColor )
168  {
169  #ifdef DEBUG
170  std::ostringstream ostr;
171  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
172  ostr << " * [BUG] NULL pointer passed for aRGBColor";
173  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
174  #endif
175 
176  return false;
177  }
178 
179  return specular.SetColor( aRGBColor );
180 }
181 
182 bool SGAPPEARANCE::SetSpecular( const SGCOLOR& aRGBColor )
183 {
184  return specular.SetColor( aRGBColor );
185 }
186 
187 
188 bool SGAPPEARANCE::SetAmbient( float aRVal, float aGVal, float aBVal )
189 {
190  return ambient.SetColor( aRVal, aGVal, aBVal );
191 }
192 
193 
194 bool SGAPPEARANCE::SetAmbient( const SGCOLOR* aRGBColor )
195 {
196  if( NULL == aRGBColor )
197  {
198  #ifdef DEBUG
199  std::ostringstream ostr;
200  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
201  ostr << " * [BUG] NULL pointer passed for aRGBColor";
202  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
203  #endif
204 
205  return false;
206  }
207 
208  return ambient.SetColor( aRGBColor );
209 }
210 
211 
212 bool SGAPPEARANCE::SetAmbient( const SGCOLOR& aRGBColor )
213 {
214  return ambient.SetColor( aRGBColor );
215 }
216 
217 
218 SGNODE* SGAPPEARANCE::FindNode(const char *aNodeName, const SGNODE *aCaller)
219 {
220  if( NULL == aNodeName || 0 == aNodeName[0] )
221  return NULL;
222 
223  if( !m_Name.compare( aNodeName ) )
224  return this;
225 
226  return NULL;
227 }
228 
229 
230 void SGAPPEARANCE::unlinkChildNode( const SGNODE* aCaller )
231 {
232  #ifdef DEBUG
233  std::ostringstream ostr;
234  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
235  ostr << " * [BUG] unexpected code branch; node should have no children or refs";
236  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
237  #endif
238 
239  return;
240 }
241 
242 
243 void SGAPPEARANCE::unlinkRefNode( const SGNODE* aCaller )
244 {
245  #ifdef DEBUG
246  std::ostringstream ostr;
247  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
248  ostr << " * [BUG] unexpected code branch; node should have no children or refs";
249  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
250  #endif
251 
252  return;
253 }
254 
255 
257 {
258  #ifdef DEBUG
259  std::ostringstream ostr;
260  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
261  ostr << " * [BUG] this node does not accept children or refs";
262  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
263  #endif
264 
265  return false;
266 }
267 
268 
270 {
271  #ifdef DEBUG
272  std::ostringstream ostr;
273  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
274  ostr << " * [BUG] this node does not accept children or refs";
275  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
276  #endif
277 
278  return false;
279 }
280 
281 
283 {
284  m_written = false;
285 
286  // rename this node
287  m_Name.clear();
288  GetName();
289 }
290 
291 
292 bool SGAPPEARANCE::WriteVRML( std::ostream& aFile, bool aReuseFlag )
293 {
294  if( aReuseFlag )
295  {
296  if( !m_written )
297  {
298  aFile << " appearance DEF " << GetName() << " Appearance {\n";
299  m_written = true;
300  }
301  else
302  {
303  aFile << " appearance USE " << GetName() << "\n";
304  return true;
305  }
306  }
307  else
308  {
309  aFile << " appearance Appearance {\n";
310  }
311 
312  aFile << " material Material {\n";
313 
314  std::string tmp;
315  float ambr, ambg, ambb;
316  ambient.GetColor( ambr, ambg, ambb );
317  float amb = ( 0.212671 * ambr + 0.71516 * ambg + 0.072169 * ambb );
318  diffuse.GetColor( ambr, ambg, ambb );
319  float den = ( 0.212671 * ambr + 0.71516 * ambg + 0.072169 * ambb );
320 
321  if( den < 0.004 )
322  den = 0.004;
323 
324  amb /= den;
325 
326  if( amb > 1.0 )
327  amb = 1.0;
328 
329  S3D::FormatFloat( tmp, amb );
330  aFile << " ambientIntensity " << tmp << "\n";
331 
332  float red, green, blue;
333  diffuse.GetColor( red, green, blue );
334  S3D::FormatFloat( tmp, red );
335  aFile << " diffuseColor " << tmp << " ";
336  S3D::FormatFloat( tmp, green );
337  aFile << tmp << " ";
338  S3D::FormatFloat( tmp, blue);
339  aFile << tmp << "\n";
340 
341  emissive.GetColor( red, green, blue );
342  S3D::FormatFloat( tmp, red );
343  aFile << " emissiveColor " << tmp << " ";
344  S3D::FormatFloat( tmp, green );
345  aFile << tmp << " ";
346  S3D::FormatFloat( tmp, blue);
347  aFile << tmp << "\n";
348 
349  S3D::FormatFloat( tmp, shininess );
350  aFile << " shininess " << tmp << "\n";
351 
352  specular.GetColor( red, green, blue );
353  S3D::FormatFloat( tmp, red );
354  aFile << " specularColor " << tmp << " ";
355  S3D::FormatFloat( tmp, green );
356  aFile << tmp << " ";
357  S3D::FormatFloat( tmp, blue);
358  aFile << tmp << "\n";
359 
361  aFile << " transparency " << tmp << "\n";
362 
363  aFile << "} }\n";
364 
365  return true;
366 }
367 
368 
369 bool SGAPPEARANCE::WriteCache( std::ostream& aFile, SGNODE* parentNode )
370 {
371  if( NULL == parentNode )
372  {
373  if( NULL == m_Parent )
374  {
375  #ifdef DEBUG
376  std::ostringstream ostr;
377  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
378  ostr << " * [BUG] corrupt data; m_aParent is NULL";
379  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
380  #endif
381 
382  return false;
383  }
384 
385  SGNODE* np = m_Parent;
386 
387  while( NULL != np->GetParent() )
388  np = np->GetParent();
389 
390  if( np->WriteCache( aFile, NULL ) )
391  {
392  m_written = true;
393  return true;
394  }
395 
396  return false;
397  }
398 
399  if( parentNode != m_Parent )
400  {
401  #ifdef DEBUG
402  std::ostringstream ostr;
403  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
404  ostr << " * [BUG] corrupt data; parentNode != m_aParent";
405  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
406  #endif
407 
408  return false;
409  }
410 
411  if( !aFile.good() )
412  {
413  #ifdef DEBUG
414  std::ostringstream ostr;
415  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
416  ostr << " * [INFO] bad stream";
417  wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() );
418  #endif
419 
420  return false;
421  }
422 
423  aFile << "[" << GetName() << "]";
424  S3D::WriteColor( aFile, ambient );
425  aFile.write( (char*)&shininess, sizeof(shininess) );
426  aFile.write( (char*)&transparency, sizeof(transparency) );
427  S3D::WriteColor( aFile, diffuse );
428  S3D::WriteColor( aFile, emissive );
429  S3D::WriteColor( aFile, specular );
430 
431  if( aFile.fail() )
432  return false;
433 
434  m_written = true;
435  return true;
436 }
437 
438 
439 bool SGAPPEARANCE::ReadCache( std::istream& aFile, SGNODE* parentNode )
440 {
441  S3D::ReadColor( aFile, ambient );
442  aFile.read( (char*)&shininess, sizeof(shininess) );
443  aFile.read( (char*)&transparency, sizeof(transparency) );
444  S3D::ReadColor( aFile, diffuse );
445  S3D::ReadColor( aFile, emissive );
446  S3D::ReadColor( aFile, specular );
447 
448  if( aFile.fail() )
449  return false;
450 
451  return true;
452 }
SGCOLOR diffuse
Definition: sg_appearance.h:40
SGAPPEARANCE(SGNODE *aParent)
bool AddChildNode(SGNODE *aNode) override
bool ReadCache(std::istream &aFile, SGNODE *parentNode) override
Function ReadCache Reads binary format data from a cache file.
SGNODE * m_Parent
Definition: sg_node.h:83
SGNODE * FindNode(const char *aNodeName, const SGNODE *aCaller) override
Function FindNode searches the tree of linked nodes and returns a reference to the first node found w...
bool ReadColor(std::istream &aFile, SGCOLOR &aColor)
Definition: sg_helpers.cpp:324
virtual ~SGAPPEARANCE()
bool SetColor(float aRedVal, float aGreenVal, float aBlueVal)
Definition: sg_base.cpp:104
virtual bool SetParent(SGNODE *aParent, bool notify=true) override
Function SetParent sets the parent SGNODE of this object.
bool WriteColor(std::ostream &aFile, const SGCOLOR &aColor)
Definition: sg_helpers.cpp:193
Class SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
S3D::SGTYPES GetNodeType(void) const
Function GetNodeType returns the type of this node instance.
Definition: sg_node.cpp:108
SGCOLOR emissive
Definition: sg_appearance.h:41
void ReNameNodes(void) override
Function ReNameNodes renames a node and all its child nodes in preparation for Write() operations...
void FormatFloat(std::string &result, double value)
Definition: sg_helpers.cpp:37
const char * GetName(void)
Definition: sg_node.cpp:150
defines the generic material appearance of a scenegraph object
SGCOLOR ambient
Definition: sg_appearance.h:39
bool m_written
Definition: sg_node.h:86
SGNODE * GetParent(void) const
Function GetParent returns a pointer to the parent SGNODE of this object or NULL if the object has no...
Definition: sg_node.cpp:114
virtual void unlinkChildNode(const SGNODE *aNode)=0
Function unlinkChild removes references to an owned child; it is invoked by the child upon destructio...
void unlinkRefNode(const SGNODE *aNode) override
Function unlinkRef removes pointers to a referenced node; it is invoked by the referenced node upon d...
float transparency
Definition: sg_appearance.h:38
std::string m_Name
Definition: sg_node.h:85
virtual bool WriteCache(std::ostream &aFile, SGNODE *parentNode)=0
Function WriteCache write's this node's data to a binary cache file; the data includes all data of ch...
bool WriteVRML(std::ostream &aFile, bool aReuseFlag) override
Function WriteVRML writes this node's data to a VRML file; this includes all data of child and refere...
bool SetSpecular(float aRVal, float aGVal, float aBVal)
S3D::SGTYPES m_SGtype
Definition: sg_node.h:84
bool SetEmissive(float aRVal, float aGVal, float aBVal)
SGCOLOR specular
Definition: sg_appearance.h:42
virtual bool AddChildNode(SGNODE *aNode)=0
bool SetAmbient(float aRVal, float aGVal, float aBVal)
void unlinkChildNode(const SGNODE *aNode) override
Function unlinkChild removes references to an owned child; it is invoked by the child upon destructio...
defines a number of macro functions to aid in repetitious code which is probably best expressed as a ...
bool WriteCache(std::ostream &aFile, SGNODE *parentNode) override
Function WriteCache write's this node's data to a binary cache file; the data includes all data of ch...
bool AddRefNode(SGNODE *aNode) override
bool SetDiffuse(float aRVal, float aGVal, float aBVal)
void GetColor(float &aRedVal, float &aGreenVal, float &aBlueVal) const
Definition: sg_base.cpp:65