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 /** 26 * @file sg_node.h 27 */ 28 29 30 #ifndef SG_NODE_H 31 #define SG_NODE_H 32 33 #include <iostream> 34 #include <string> 35 #include <list> 36 #include <vector> 37 #include <map> 38 #include <glm/glm.hpp> 39 40 #include "plugins/3dapi/c3dmodel.h" 41 #include "plugins/3dapi/sg_base.h" 42 #include "plugins/3dapi/sg_types.h" 43 44 class SGNODE; 45 class SGAPPEARANCE; 46 47 namespace S3D 48 { 49 /** 50 * Return the name of the given type of node 51 */ 52 char const* GetNodeTypeName( S3D::SGTYPES aType ) noexcept; 53 54 struct MATLIST 55 { 56 std::vector< SGAPPEARANCE const* > matorder; // materials in order of addition 57 std::map< SGAPPEARANCE const*, int > matmap; // mapping from material to index 58 }; 59 60 bool GetMatIndex( MATLIST& aList, SGNODE* aNode, int& aIndex ); 61 62 void INIT_SMATERIAL( SMATERIAL& aMaterial ); 63 void INIT_SMESH( SMESH& aMesh ) noexcept; 64 void INIT_S3DMODEL( S3DMODEL& aModel ) noexcept; 65 66 void FREE_SMESH( SMESH& aMesh) noexcept; 67 void FREE_S3DMODEL( S3DMODEL& aModel ); 68 } 69 70 71 /** 72 * The base class of all Scene Graph nodes. 73 */ 74 class SGNODE 75 { 76 public: 77 SGNODE( SGNODE* aParent ); 78 virtual ~SGNODE(); 79 80 /** 81 * Return the type of this node instance. 82 */ 83 S3D::SGTYPES GetNodeType( void ) const noexcept; 84 85 /** 86 * Returns a pointer to the parent SGNODE of this object or NULL if the object has 87 * no parent (ie. top level transform). 88 */ 89 SGNODE* GetParent( void ) const noexcept; 90 91 /** 92 * Set the parent #SGNODE of this object. 93 * 94 * @param aParent [in] is the desired parent node 95 * @return true if the operation succeeds; false if the given node is not allowed to 96 * be a parent to the derived object. 97 */ 98 virtual bool SetParent( SGNODE* aParent, bool notify = true ) = 0; 99 100 /** 101 * Swap the ownership with the given parent. 102 * 103 * This operation may be required when reordering nodes for optimization. 104 * 105 * @param aNewParent will become the new parent to the object; it must be the same type 106 * as the parent of this instance. 107 */ 108 bool SwapParent( SGNODE* aNewParent ); 109 110 const char* GetName( void ); 111 void SetName(const char *aName); 112 113 const char * GetNodeTypeName( S3D::SGTYPES aNodeType ) const noexcept; 114 115 /** 116 * Search the tree of linked nodes and return a reference to the first node found with 117 * the given name. 118 * 119 * The reference is then typically added to another node via AddRefNode(). 120 * 121 * @param aNodeName is the name of the node to search for. 122 * @param aCaller is a pointer to the node invoking this function. 123 * @return is a valid node pointer on success, otherwise NULL. 124 */ 125 virtual SGNODE* FindNode( const char *aNodeName, const SGNODE *aCaller ) = 0; 126 127 virtual bool AddRefNode( SGNODE* aNode ) = 0; 128 129 virtual bool AddChildNode( SGNODE* aNode ) = 0; 130 131 /** 132 * Associate this object with a handle to itself. 133 * 134 * The handle is typically held by an IFSG* wrapper and the pointer which it refers to 135 * is set to NULL upon destruction of this object. This mechanism provides a scheme 136 * by which a wrapper can be notified of the destruction of the object which it wraps. 137 */ 138 void AssociateWrapper( SGNODE** aWrapperRef ) noexcept; 139 140 /** 141 * Remove the association between an IFSG* wrapper object and this object. 142 */ 143 void DisassociateWrapper( SGNODE** aWrapperRef ) noexcept; 144 145 /** 146 * Reset the global SG* node indices in preparation for write operations. 147 */ 148 void ResetNodeIndex( void ) noexcept; 149 150 /** 151 * Rename a node and all its child nodes in preparation for write operations. 152 */ 153 virtual void ReNameNodes( void ) = 0; 154 155 /** 156 * Writes this node's data to a VRML file. 157 * 158 * This includes all data of child and referenced nodes. 159 */ 160 virtual bool WriteVRML( std::ostream& aFile, bool aReuseFlag ) = 0; 161 162 /** 163 * Write this node's data to a binary cache file. 164 * 165 * The data includes all data of children and references to children. If this function 166 * is invoked by the user, parentNode must be set to NULL in order to ensure coherent data. 167 */ 168 virtual bool WriteCache( std::ostream& aFile, SGNODE* parentNode ) = 0; 169 170 /** 171 * Reads binary format data from a cache file. 172 * 173 * To read a cache file, open the file for reading and invoke this function from a new 174 * #SCENEGRAPH node. 175 */ 176 virtual bool ReadCache( std::istream& aFile, SGNODE* parentNode ) = 0; 177 178 /** 179 * Remove references to an owned child. 180 * 181 * This is invoked by the child upon destruction to ensure that the parent has no 182 * invalid references. 183 * 184 * @param aNode is the child which is being deleted. 185 */ 186 virtual void unlinkChildNode( const SGNODE* aNode ) = 0; 187 188 /** 189 * Remove pointers to a referenced node. 190 * 191 * This is invoked by the referenced node upon destruction to ensure that the referring 192 * node has no invalid references. 193 * 194 * @param aNode is the node which is being deleted. 195 */ 196 virtual void unlinkRefNode( const SGNODE* aNode ) = 0; 197 198 /** 199 * Add a pointer to a node which references this node, but does not own. 200 * 201 * Such back-pointers are required to ensure that invalidated references are removed 202 * when a node is deleted. 203 * 204 * @param aNode is the node holding a reference to this object. 205 */ 206 void addNodeRef( SGNODE* aNode ); 207 208 /** 209 * Remove a pointer to a node which references this node, but does not own. 210 * 211 * @param aNode is the node holding a reference to this object. 212 */ 213 void delNodeRef( const SGNODE* aNode ); 214 215 /** 216 * Return true if the object had already been written to a cache file or VRML file 217 * 218 * For internal use only. 219 */ isWritten(void)220 bool isWritten( void ) noexcept 221 { 222 return m_written; 223 } 224 225 protected: 226 std::list< SGNODE* > m_BackPointers; ///< nodes which hold a reference to this. 227 SGNODE* m_Parent; ///< Pointer to parent node; may be NULL for top level transform. 228 S3D::SGTYPES m_SGtype; ///< Type of Scene Graph node. 229 std::string m_Name; ///< name to use for referencing the entity by name. 230 bool m_written; ///< Set to true when the object has been written after a ReNameNodes(). 231 232 private: 233 SGNODE** m_Association; ///< Handle to the instance held by a wrapper. 234 }; 235 236 #endif // SG_NODE_H 237