KiCad PCB EDA Suite
vrml2_color.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) 2016 Cirilo Bernardo <[email protected]>
5 * Copyright (C) 2021 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
26#include <iostream>
27#include <sstream>
28#include <wx/log.h>
29
30#include "vrml2_base.h"
31#include "vrml2_color.h"
32
33
35{
37}
38
39
41{
43 m_Parent = aParent;
44
45 if( nullptr != m_Parent )
46 m_Parent->AddChildNode( this );
47}
48
49
51{
52 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying Color node" ) );
53}
54
55
57{
58 // this node is dangling unless it has a parent of type WRL2_INDEXEDFACESET
60 return true;
61
62 return false;
63}
64
65
67{
68 // this node may not own or reference any other node
69 wxCHECK_MSG( false, false, wxT( "AddRefNode is not applicable." ) );
70}
71
72
74{
75 // this node may not own or reference any other node
76 wxCHECK_MSG( false, false, wxT( "AddChildNode is not applicable." ) );
77}
78
79
80bool WRL2COLOR::Read( WRLPROC& proc, WRL2BASE* aTopNode )
81{
82 char tok = proc.Peek();
83
84 if( proc.eof() )
85 {
86 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
87 " * [INFO] bad file format; unexpected eof %s." ),
88 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
89
90 return false;
91 }
92
93 if( '{' != tok )
94 {
95 wxLogTrace( traceVrmlPlugin,
96 wxT( "%s:%s:%d\n"
97 " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
98 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
99
100 return false;
101 }
102
103 proc.Pop();
104 std::string glob;
105
106 if( proc.Peek() == '}' )
107 {
108 proc.Pop();
109 return true;
110 }
111
112 if( !proc.ReadName( glob ) )
113 {
114 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
115 "%s" ),
116 __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
117
118 return false;
119 }
120
121 // expecting 'color'
122 if( !glob.compare( "color" ) )
123 {
124 if( !proc.ReadMFVec3f( colors ) )
125 {
126 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
127 " * [INFO] invalid color set %s\n"
128 " * [INFO] file: '%s'\n"
129 "%s" ),
130 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
131 proc.GetFileName(), proc.GetError() );
132
133 return false;
134 }
135 }
136 else
137 {
138 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
139 " * [INFO] invalid Color %s\n"
140 " * [INFO] file: '%s'\n" ),
141 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(), proc.GetFileName() );
142
143 return false;
144 }
145
146 if( proc.Peek() == '}' )
147 {
148 proc.Pop();
149 return true;
150 }
151
152 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
153 " * [INFO] invalid Color %s (no closing brace)\n"
154 " * [INFO] file: '%s'\n" ),
155 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(), proc.GetFileName() );
156
157 return false;
158}
159
160
162{
163 // any data manipulation must be performed by the parent node
164 return nullptr;
165}
166
167
169{
170 if( colors.empty() )
171 return false;
172
173 return true;
174}
175
176
177void WRL2COLOR::GetColor( int aIndex, float& red, float& green, float& blue )
178{
179 if( aIndex < 0 || aIndex >= (int)colors.size() )
180 {
181 red = 0.8f;
182 green = 0.8f;
183 blue = 0.8f;
184 return;
185 }
186
187 red = colors[aIndex].x;
188 green = colors[aIndex].y;
189 blue = colors[aIndex].z;
190}
191
192
193void WRL2COLOR::GetColors( WRLVEC3F*& aColorList, size_t& aListSize)
194{
195 if( colors.empty() )
196 {
197 aColorList = nullptr;
198 aListSize = 0;
199 return;
200 }
201
202 aColorList = &colors[0];
203 aListSize = colors.size();
204}
The base class of all Scene Graph nodes.
Definition: sg_node.h:75
The top node of a VRML2 model.
Definition: vrml2_base.h:60
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
Definition: vrml2_color.cpp:80
std::vector< WRLVEC3F > colors
Definition: vrml2_color.h:70
void GetColor(int aIndex, float &red, float &green, float &blue)
Retrieve the given color (or default 0.8, 0.8, 0.8 if index is invalid).
bool HasColors(void)
bool AddChildNode(WRL2NODE *aNode) override
Definition: vrml2_color.cpp:73
bool AddRefNode(WRL2NODE *aNode) override
Definition: vrml2_color.cpp:66
bool isDangling(void) override
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
Definition: vrml2_color.cpp:56
virtual ~WRL2COLOR()
Definition: vrml2_color.cpp:50
void GetColors(WRLVEC3F *&aColorList, size_t &aListSize)
Retrieve the current list of colors.
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
WRL2NODE * m_Parent
Definition: vrml2_node.h:169
WRL2NODES m_Type
Definition: vrml2_node.h:170
WRL2NODES GetNodeType(void) const
Definition: vrml2_node.cpp:204
virtual bool AddChildNode(WRL2NODE *aNode)
Definition: vrml2_node.cpp:356
void Pop(void)
Definition: wrlproc.cpp:2035
char Peek(void)
Definition: wrlproc.cpp:2007
std::string GetFileName(void)
Definition: wrlproc.cpp:1995
std::string GetError(void)
Definition: wrlproc.cpp:1960
bool eof(void)
Definition: wrlproc.cpp:1954
bool ReadMFVec3f(std::vector< WRLVEC3F > &aMFVec3f)
Definition: wrlproc.cpp:1839
bool ReadName(std::string &aName)
Definition: wrlproc.cpp:289
std::string GetFilePosition() const
Definition: wrlproc.cpp:1982
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition: vrml.cpp:63
@ WRL2_INDEXEDFACESET
glm::vec3 WRLVEC3F
Definition: wrltypes.h:188