KiCad PCB EDA Suite
sg_colors.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 <sstream>
27 #include <wx/log.h>
28 
29 #include "3d_cache/sg/sg_colors.h"
30 #include "3d_cache/sg/sg_helpers.h"
31 
32 
33 SGCOLORS::SGCOLORS( SGNODE* aParent ) : SGNODE( aParent )
34 {
36 
37  if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
38  {
39  m_Parent = nullptr;
40 
41  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [BUG] inappropriate parent to SGCOLORS (type %s)",
42  __FILE__, __FUNCTION__, __LINE__, aParent->GetNodeType() );
43  }
44  else if( nullptr != aParent && S3D::SGTYPE_FACESET == aParent->GetNodeType() )
45  {
46  m_Parent->AddChildNode( this );
47  }
48 }
49 
50 
52 {
53  colors.clear();
54 }
55 
56 
57 bool SGCOLORS::SetParent( SGNODE* aParent, bool notify )
58 {
59  if( nullptr != m_Parent )
60  {
61  if( aParent == m_Parent )
62  return true;
63 
64  // handle the change in parents
65  if( notify )
66  m_Parent->unlinkChildNode( this );
67 
68  m_Parent = nullptr;
69 
70  if( nullptr == aParent )
71  return true;
72  }
73 
74  // only a SGFACESET may be parent to a SGCOLORS
75  if( nullptr != aParent && S3D::SGTYPE_FACESET != aParent->GetNodeType() )
76  return false;
77 
78  m_Parent = aParent;
79 
80  if( m_Parent )
81  m_Parent->AddChildNode( this );
82 
83  return true;
84 }
85 
86 
87 SGNODE* SGCOLORS::FindNode(const char* aNodeName, const SGNODE *aCaller) noexcept
88 {
89  if( nullptr == aNodeName || 0 == aNodeName[0] )
90  return nullptr;
91 
92  if( !m_Name.compare( aNodeName ) )
93  return this;
94 
95  return nullptr;
96 }
97 
98 
99 void SGCOLORS::unlinkChildNode( const SGNODE* aCaller ) noexcept
100 {
101  wxCHECK( aCaller, /* void */ );
102 }
103 
104 
105 void SGCOLORS::unlinkRefNode( const SGNODE* aCaller ) noexcept
106 {
107  wxCHECK( aCaller, /* void */ );
108 }
109 
110 
111 bool SGCOLORS::AddRefNode( SGNODE* aNode ) noexcept
112 {
113  wxCHECK( aNode, false );
114 
115  return false;
116 }
117 
118 
119 bool SGCOLORS::AddChildNode( SGNODE* aNode ) noexcept
120 {
121  wxCHECK( aNode, false );
122 
123  return false;
124 }
125 
126 
127 bool SGCOLORS::GetColorList( size_t& aListSize, SGCOLOR*& aColorList )
128 {
129  if( colors.empty() )
130  {
131  aListSize = 0;
132  aColorList = nullptr;
133  return false;
134  }
135 
136  aListSize = colors.size();
137  aColorList = &colors[0];
138  return true;
139 }
140 
141 
142 void SGCOLORS::SetColorList( size_t aListSize, const SGCOLOR* aColorList )
143 {
144  colors.clear();
145 
146  if( 0 == aListSize || nullptr == aColorList )
147  return;
148 
149  for( size_t i = 0; i < aListSize; ++i )
150  colors.push_back( aColorList[i] );
151 
152  return;
153 }
154 
155 
156 void SGCOLORS::AddColor( double aRedValue, double aGreenValue, double aBlueValue )
157 {
158  colors.emplace_back( aRedValue, aGreenValue, aBlueValue );
159  return;
160 }
161 
162 
163 void SGCOLORS::AddColor( const SGCOLOR& aColor )
164 {
165  colors.push_back( aColor );
166  return;
167 }
168 
169 
171 {
172  m_written = false;
173 
174  // rename this node
175  m_Name.clear();
176  GetName();
177 }
178 
179 
180 bool SGCOLORS::WriteVRML( std::ostream& aFile, bool aReuseFlag )
181 {
182  if( colors.empty() )
183  return false;
184 
185  if( aReuseFlag )
186  {
187  if( !m_written )
188  {
189  aFile << "color DEF " << GetName() << " Color { color [\n ";
190  m_written = true;
191  }
192  else
193  {
194  aFile << "color USE " << GetName() << "\n";
195  return true;
196  }
197  }
198  else
199  {
200  aFile << "color Color { color [\n ";
201  }
202 
203  std::string tmp;
204  size_t n = colors.size();
205  bool nline = false;
206 
207  for( size_t i = 0; i < n; )
208  {
209  S3D::FormatColor( tmp, colors[i] );
210  float r,g,b;
211  colors[i].GetColor(r, g, b);
212  aFile << tmp ;
213  ++i;
214 
215  if( i < n )
216  {
217  aFile << ",";
218 
219  if( nline )
220  {
221  aFile << "\n ";
222  nline = false;
223  }
224  else
225  {
226  nline = true;
227  }
228 
229  }
230  }
231 
232  aFile << "] }\n";
233 
234  return true;
235 }
236 
237 
238 bool SGCOLORS::WriteCache( std::ostream& aFile, SGNODE* parentNode )
239 {
240  if( nullptr == parentNode )
241  {
242  wxCHECK( m_Parent, false );
243 
244  SGNODE* np = m_Parent;
245 
246  while( nullptr != np->GetParent() )
247  np = np->GetParent();
248 
249  if( np->WriteCache( aFile, nullptr ) )
250  {
251  m_written = true;
252  return true;
253  }
254 
255  return false;
256  }
257 
258  wxCHECK( parentNode == m_Parent, false );
259 
260  if( !aFile.good() )
261  {
262  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [INFO] bad stream", __FILE__, __FUNCTION__, __LINE__ );
263 
264  return false;
265  }
266 
267  aFile << "[" << GetName() << "]";
268  size_t ncolors = colors.size();
269  aFile.write( (char*)&ncolors, sizeof(size_t) );
270 
271  for( size_t i = 0; i < ncolors; ++i )
272  S3D::WriteColor( aFile, colors[i] );
273 
274  if( aFile.fail() )
275  return false;
276 
277  m_written = true;
278  return true;
279 }
280 
281 
282 bool SGCOLORS::ReadCache( std::istream& aFile, SGNODE* parentNode )
283 {
284  wxCHECK( colors.empty(), false );
285 
286  size_t ncolors;
287  aFile.read( (char*) &ncolors, sizeof( size_t ) );
288  SGCOLOR tmp;
289 
290  if( aFile.fail() )
291  return false;
292 
293  for( size_t i = 0; i < ncolors; ++i )
294  {
295  if( !S3D::ReadColor( aFile, tmp ) || aFile.fail() )
296  return false;
297 
298  colors.push_back( tmp );
299  }
300 
301  return true;
302 }
virtual bool SetParent(SGNODE *aParent, bool notify=true) override
Set the parent SGNODE of this object.
Definition: sg_colors.cpp:57
void AddColor(double aRedValue, double aGreenValue, double aBlueValue)
Definition: sg_colors.cpp:156
bool AddRefNode(SGNODE *aNode) noexcept override
Definition: sg_colors.cpp:111
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 GetColorList(size_t &aListSize, SGCOLOR *&aColorList)
Definition: sg_colors.cpp:127
void FormatColor(std::string &result, const SGCOLOR &aColor)
Definition: sg_helpers.cpp:135
bool ReadCache(std::istream &aFile, SGNODE *parentNode) override
Reads binary format data from a cache file.
Definition: sg_colors.cpp:282
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
void ReNameNodes(void) override
Rename a node and all its child nodes in preparation for write operations.
Definition: sg_colors.cpp:170
const char * GetName(void)
Definition: sg_node.cpp:146
bool m_written
Set to true when the object has been written after a ReNameNodes().
Definition: sg_node.h:230
void unlinkChildNode(const SGNODE *aNode) noexcept override
Remove references to an owned child.
Definition: sg_colors.cpp:99
virtual void unlinkChildNode(const SGNODE *aNode)=0
Remove references to an owned child.
bool WriteVRML(std::ostream &aFile, bool aReuseFlag) override
Writes this node's data to a VRML file.
Definition: sg_colors.cpp:180
std::vector< SGCOLOR > colors
Definition: sg_colors.h:64
void unlinkRefNode(const SGNODE *aNode) noexcept override
Remove pointers to a referenced node.
Definition: sg_colors.cpp:105
virtual ~SGCOLORS()
Definition: sg_colors.cpp:51
E_SERIE r
Definition: eserie.cpp:41
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.
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.
Definition: sg_colors.cpp:87
S3D::SGTYPES m_SGtype
Type of Scene Graph node.
Definition: sg_node.h:228
bool WriteCache(std::ostream &aFile, SGNODE *parentNode) override
Write this node's data to a binary cache file.
Definition: sg_colors.cpp:238
void SetColorList(size_t aListSize, const SGCOLOR *aColorList)
Definition: sg_colors.cpp:142
virtual bool AddChildNode(SGNODE *aNode)=0
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...
bool AddChildNode(SGNODE *aNode) noexcept override
Definition: sg_colors.cpp:119
SGCOLORS(SGNODE *aParent)
Definition: sg_colors.cpp:33
S3D::SGTYPES GetNodeType(void) const noexcept
Return the type of this node instance.
Definition: sg_node.cpp:104