SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MeshConstruction.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9  License for the specific language governing rights and limitations under
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 /// @todo Documentation Modules/Visualization/MeshConstruction.h
29 
30 #ifndef MODULES_VISUALIZATION_MESH_CONSTRUCTION_H
31 #define MODULES_VISUALIZATION_MESH_CONSTRUCTION_H
32 
34 #include <Core/Datatypes/Mesh/VMesh.h>
35 #include <Core/Datatypes/Mesh/Mesh.h>
40 
41 namespace SCIRun {
42 namespace Modules {
43 namespace Visualization {
44 
45 namespace MeshConstruction
46 {
47  /// Constructs faces without normal information. We can share the primary
48  /// VBO with the nodes and the edges in this case.
49  template <typename VMeshType>
50  static void buildFacesNoNormals(typename SCIRun::Core::Datatypes::MeshTraits<VMeshType>::MeshFacadeHandle facade,
52  const std::string& primaryVBOName,
53  ModuleStateHandle state)
54  {
55  bool faceTransparency = state->getValue(ShowFieldModule::FaceTransparency).getBool();
56  uint32_t* iboFaces = nullptr;
57 
58  // Determine the size of the face structure (taking into account the varying
59  // types of faces -- only quads and triangles are currently supported).
60  size_t iboFacesSize = 0;
61  BOOST_FOREACH(const FaceInfo<VMeshType>& face, facade->faces())
62  {
63  typename VMeshType::Node::array_type nodes = face.nodeIndices();
64  if (nodes.size() == 4)
65  {
66  iboFacesSize += sizeof(uint32_t) * 6;
67  }
68  else if (nodes.size() == 3)
69  {
70  iboFacesSize += sizeof(uint32_t) * 3;
71  }
72  else
73  {
74  BOOST_THROW_EXCEPTION(SCIRun::Core::DimensionMismatch()
75  << SCIRun::Core::ErrorMessage("Only Quads and Triangles are supported."));
76  }
77  }
78  std::shared_ptr<std::vector<uint8_t>> rawIBO(new std::vector<uint8_t>());
79  rawIBO->resize(iboFacesSize); // Linear in complexity... If we need more performance,
80  // use malloc to generate buffer and then vector::assign.
81  iboFaces = reinterpret_cast<uint32_t*>(&(*rawIBO)[0]);
82  size_t i = 0;
83  BOOST_FOREACH(const FaceInfo<VMeshType>& face, facade->faces())
84  {
85  typename VMeshType::Node::array_type nodes = face.nodeIndices();
86  if (nodes.size() == 4)
87  {
88  // Winding order looks good from tests.
89  iboFaces[i ] = static_cast<uint32_t>(nodes[0]); iboFaces[i+1] = static_cast<uint32_t>(nodes[1]); iboFaces[i+2] = static_cast<uint32_t>(nodes[2]);
90  iboFaces[i+3] = static_cast<uint32_t>(nodes[0]); iboFaces[i+4] = static_cast<uint32_t>(nodes[2]); iboFaces[i+5] = static_cast<uint32_t>(nodes[3]);
91  i += 6;
92  }
93  else if (nodes.size() == 3)
94  {
95  iboFaces[i ] = static_cast<uint32_t>(nodes[0]); iboFaces[i+1] = static_cast<uint32_t>(nodes[1]); iboFaces[i+2] = static_cast<uint32_t>(nodes[2]);
96  i += 3;
97  }
98  // All other cases have been checked in the loop above which determines
99  // the size of the face IBO.
100  }
101 
102  // Add IBO for the faces.
103  std::string facesIBOName = "facesIBO";
104  geom->mIBOs.emplace_back(GeometryObject::SpireIBO(facesIBOName, sizeof(uint32_t), rawIBO));
105 
106  // Build pass for the faces.
107  /// \todo Find an appropriate place to put program names like UniformColor.
108  GeometryObject::SpireSubPass pass =
109  GeometryObject::SpireSubPass("facesPass", primaryVBOName,
110  facesIBOName, "UniformColor",
111  Spire::StuInterface::TRIANGLES);
112  if (faceTransparency)
113  pass.addUniform("uColor", Spire::V4(1.0f, 1.0f, 1.0f, 0.2f));
114  else
115  pass.addUniform("uColor", Spire::V4(1.0f, 1.0f, 1.0f, 1.0f));
116 
117  geom->mPasses.emplace_back(pass);
118  }
119 
120  /// Constructs edges without normal information. We can share the primary
121  /// VBO with faces and nodes.
122  template <typename VMeshType>
125  const std::string& primaryVBOName,
126  ModuleStateHandle state);
127 
128  /// Constructs nodes without normal information. We can share the primary
129  /// VBO with edges and faces.
130  template <typename VMeshType>
133  const std::string& primaryVBOName,
134  ModuleStateHandle state);
135 };
136 
137 }}}
138 
139 #endif
140 
boost::shared_ptr< ModuleStateInterface > ModuleStateHandle
Definition: NetworkFwd.h:75
boost::shared_ptr< GeometryObject > GeometryHandle
Definition: DatatypeFwd.h:61
boost::error_info< struct tag_error_message, std::string > ErrorMessage
Definition: Exception.h:52
void buildNodesNoNormals(typename SCIRun::Core::Datatypes::MeshTraits< VMeshType >::MeshFacadeHandle facade, SCIRun::Core::Datatypes::GeometryHandle geom, const std::string &primaryVBOName, ModuleStateHandle state)
Definition: Exception.h:78
static Core::Algorithms::AlgorithmParameterName FaceTransparency
Definition: ShowField.h:55
void buildEdgesNoNormals(typename SCIRun::Core::Datatypes::MeshTraits< VMeshType >::MeshFacadeHandle facade, SCIRun::Core::Datatypes::GeometryHandle geom, const std::string &primaryVBOName, ModuleStateHandle state)
boost::shared_ptr< MeshFacade< VirtualMeshType > > MeshFacadeHandle
Definition: MeshTraits.h:61