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