KiCad PCB EDA Suite
sg_normals.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_normals.h"
30 #include "3d_cache/sg/sg_helpers.h"
31 
32 
33 SGNORMALS::SGNORMALS( 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 SGNORMALS (type %d)",
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  norms.clear();
54 }
55 
56 
57 bool SGNORMALS::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 SGNORMALS
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* SGNORMALS::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 SGNORMALS::unlinkChildNode( const SGNODE* aCaller ) noexcept
100 {
101  wxCHECK( false, /* void */ );
102 }
103 
104 
105 void SGNORMALS::unlinkRefNode( const SGNODE* aCaller ) noexcept
106 {
107  wxCHECK( false, /* void */ );
108 }
109 
110 
111 bool SGNORMALS::AddRefNode( SGNODE* aNode ) noexcept
112 {
113  wxCHECK( false, false );
114 
115  return false;
116 }
117 
118 
119 bool SGNORMALS::AddChildNode( SGNODE* aNode ) noexcept
120 {
121  wxCHECK( false, false );
122 
123  return false;
124 }
125 
126 
127 bool SGNORMALS::GetNormalList( size_t& aListSize, SGVECTOR*& aNormalList )
128 {
129  if( norms.empty() )
130  {
131  aListSize = 0;
132  aNormalList = nullptr;
133  return false;
134  }
135 
136  aListSize = norms.size();
137  aNormalList = &norms[0];
138  return true;
139 }
140 
141 
142 void SGNORMALS::SetNormalList( size_t aListSize, const SGVECTOR* aNormalList )
143 {
144  norms.clear();
145 
146  if( 0 == aListSize || nullptr == aNormalList )
147  return;
148 
149  for( int i = 0; i < (int)aListSize; ++i )
150  norms.push_back( aNormalList[i] );
151 }
152 
153 
154 void SGNORMALS::AddNormal( double aXValue, double aYValue, double aZValue )
155 {
156  norms.emplace_back( aXValue, aYValue, aZValue );
157 }
158 
159 
160 void SGNORMALS::AddNormal( const SGVECTOR& aNormal )
161 {
162  norms.push_back( aNormal );
163 }
164 
165 
167 {
168  m_written = false;
169 
170  // rename this node
171  m_Name.clear();
172  GetName();
173 }
174 
175 
176 bool SGNORMALS::WriteVRML( std::ostream& aFile, bool aReuseFlag )
177 {
178  if( norms.empty() )
179  return false;
180 
181  if( aReuseFlag )
182  {
183  if( !m_written )
184  {
185  aFile << " normal DEF " << GetName() << " Normal { vector [\n ";
186  m_written = true;
187  }
188  else
189  {
190  aFile << " normal USE " << GetName() << "\n";
191  return true;
192  }
193  }
194  else
195  {
196  aFile << " normal Normal { vector [\n ";
197  }
198 
199  std::string tmp;
200  size_t n = norms.size();
201  bool nline = false;
202 
203  for( size_t i = 0; i < n; )
204  {
205  S3D::FormatVector( tmp, norms[i] );
206  aFile << tmp ;
207  ++i;
208 
209  if( i < n )
210  {
211  aFile << ",";
212 
213  if( nline )
214  {
215  aFile << "\n ";
216  nline = false;
217  }
218  else
219  {
220  nline = true;
221  }
222 
223  }
224  }
225 
226  aFile << "] }\n";
227 
228  return true;
229 }
230 
231 
232 bool SGNORMALS::WriteCache( std::ostream& aFile, SGNODE* parentNode )
233 {
234  if( nullptr == parentNode )
235  {
236  wxCHECK( m_Parent, false );
237 
238  SGNODE* np = m_Parent;
239 
240  while( nullptr != np->GetParent() )
241  np = np->GetParent();
242 
243  if( np->WriteCache( aFile, nullptr ) )
244  {
245  m_written = true;
246  return true;
247  }
248 
249  return false;
250  }
251 
252  wxCHECK( parentNode == m_Parent, false );
253 
254  if( !aFile.good() )
255  {
256  wxLogTrace( MASK_3D_SG, "%s:%s:%d * [INFO] bad stream",
257  __FILE__, __FUNCTION__, __LINE__ );
258 
259  return false;
260  }
261 
262  aFile << "[" << GetName() << "]";
263  size_t npts = norms.size();
264  aFile.write( (char*)&npts, sizeof(size_t) );
265 
266  for( size_t i = 0; i < npts; ++i )
267  S3D::WriteVector( aFile, norms[i] );
268 
269  if( aFile.fail() )
270  return false;
271 
272  m_written = true;
273  return true;
274 }
275 
276 
277 bool SGNORMALS::ReadCache( std::istream& aFile, SGNODE* parentNode )
278 {
279  wxCHECK( norms.empty(), false );
280 
281  size_t npts;
282  aFile.read( (char*) &npts, sizeof( size_t ) );
283  SGVECTOR tmp;
284 
285  if( aFile.fail() )
286  return false;
287 
288  for( size_t i = 0; i < npts; ++i )
289  {
290  if( !S3D::ReadVector( aFile, tmp ) || aFile.fail() )
291  return false;
292 
293  norms.push_back( tmp );
294  }
295 
296  return true;
297 }
bool AddChildNode(SGNODE *aNode) noexcept override
Definition: sg_normals.cpp:119
void ReNameNodes(void) override
Rename a node and all its child nodes in preparation for write operations.
Definition: sg_normals.cpp:166
bool WriteCache(std::ostream &aFile, SGNODE *parentNode) override
Write this node's data to a binary cache file.
Definition: sg_normals.cpp:232
SGNODE * m_Parent
Pointer to parent node; may be NULL for top level transform.
Definition: sg_node.h:227
void unlinkChildNode(const SGNODE *aNode) noexcept override
Remove references to an owned child.
Definition: sg_normals.cpp:99
virtual bool SetParent(SGNODE *aParent, bool notify=true) override
Set the parent SGNODE of this object.
Definition: sg_normals.cpp:57
bool ReadVector(std::istream &aFile, SGVECTOR &aVector)
Definition: sg_helpers.cpp:279
void unlinkRefNode(const SGNODE *aNode) noexcept override
Remove pointers to a referenced node.
Definition: sg_normals.cpp:105
The base class of all Scene Graph nodes.
Definition: sg_node.h:74
bool ReadCache(std::istream &aFile, SGNODE *parentNode) override
Reads binary format data from a cache file.
Definition: sg_normals.cpp:277
virtual ~SGNORMALS()
Definition: sg_normals.cpp:51
bool WriteVRML(std::ostream &aFile, bool aReuseFlag) override
Writes this node's data to a VRML file.
Definition: sg_normals.cpp:176
void FormatVector(std::string &result, const SGVECTOR &aVector)
Definition: sg_helpers.cpp:118
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
virtual void unlinkChildNode(const SGNODE *aNode)=0
Remove references to an owned child.
bool WriteVector(std::ostream &aFile, const SGVECTOR &aVector)
Definition: sg_helpers.cpp:165
std::vector< SGVECTOR > norms
Definition: sg_normals.h:64
void AddNormal(double aXValue, double aYValue, double aZValue)
Definition: sg_normals.cpp:154
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 AddRefNode(SGNODE *aNode) noexcept override
Definition: sg_normals.cpp:111
S3D::SGTYPES m_SGtype
Type of Scene Graph node.
Definition: sg_node.h:228
virtual bool AddChildNode(SGNODE *aNode)=0
Define a number of macros to aid in repetitious code which is probably best expressed as a preprocess...
void SetNormalList(size_t aListSize, const SGVECTOR *aNormalList)
Definition: sg_normals.cpp:142
bool GetNormalList(size_t &aListSize, SGVECTOR *&aNormalList)
Definition: sg_normals.cpp:127
S3D::SGTYPES GetNodeType(void) const noexcept
Return the type of this node instance.
Definition: sg_node.cpp:104
SGNORMALS(SGNODE *aParent)
Definition: sg_normals.cpp:33
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_normals.cpp:87