My Project
 All Classes Files Functions Variables Enumerations Pages
triangle_mesh.h
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Copyright 2009 Tim Day */
3 /* */
4 /* This file is part of Fracplanet */
5 /* */
6 /* Fracplanet is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* Fracplanet is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with Fracplanet. If not, see <http://www.gnu.org/licenses/>. */
18 /**************************************************************************/
19 
20 #ifndef _triangle_mesh_h_
21 #define _triangle_mesh_h_
22 
23 #include "geometry.h"
24 #include "parameters_save.h"
25 #include "parameters_terrain.h"
26 #include "progress.h"
27 #include "random.h"
28 #include "triangle.h"
29 #include "triangle_edge.h"
30 #include "vertex.h"
31 
36 
37 
50 {
51 public:
52 
54  TriangleMesh(Progress* progress);
55 
57  virtual ~TriangleMesh();
58 
60  void set_emissive(float e)
61  {
62  _emissive=e;
63  }
64 
66  float emissive() const
67  {
68  return _emissive;
69  }
70 
72  void add_vertex(const Vertex& v)
73  {
74  _vertex.push_back(v);
75  }
76 
78  void add_triangle(const Triangle& t)
79  {
80  _triangle.push_back(t);
81  }
82 
84  const Vertex& vertex(uint i) const
85  {
86  return _vertex[i];
87  }
88 
90  const Triangle& triangle(uint i) const
91  {
92  return _triangle[i];
93  }
94 
96  virtual const Geometry& geometry() const
97  =0;
98 
100  float vertex_height(uint i) const
101  {
102  return geometry().height(vertex(i).position());
103  }
104 
106  void set_vertex_height(uint i,float h)
107  {
108  XYZ p(vertex(i).position());
109  geometry().set_height(p,h);
110  vertex(i).position(p);
111  }
112 
114  float triangle_height_min(uint i) const
115  {
116  const Triangle& t=triangle(i);
117  return minimum
118  (
119  vertex_height(t.vertex(0)),
120  vertex_height(t.vertex(1)),
121  vertex_height(t.vertex(2))
122  );
123  }
124 
126  float triangle_height_max(uint i) const
127  {
128  const Triangle& t=triangle(i);
129  return maximum
130  (
131  vertex_height(t.vertex(0)),
132  vertex_height(t.vertex(1)),
133  vertex_height(t.vertex(2))
134  );
135  }
136 
138  float triangle_height_average(uint i) const
139  {
140  const Triangle& t=triangle(i);
141  return
142  (
143  vertex_height(t.vertex(0))
144  +vertex_height(t.vertex(1))
145  +vertex_height(t.vertex(2))
146  )/3.0;
147  }
148 
150  const XYZ triangle_normal(uint i) const;
151 
153  uint which_colour_for_triangle(uint t) const
154  {
155  return (t<_triangle_switch_colour ? 0 : 1);
156  }
157 
159  uint vertices() const
160  {
161  return _vertex.size();
162  }
164  uint triangles() const
165  {
166  return _triangle.size();
167  }
168 
170  uint triangles_of_colour0() const
171  {
173  }
174 
176  uint triangles_of_colour1() const
177  {
179  }
180 
182  void compute_vertex_normals();
183 
185  void subdivide(const XYZ& variation,uint level,uint levels);
186 
188  void subdivide(uint subdivisions,uint flat_subdivisions,const XYZ& variation);
189 
191  void write_povray(std::ofstream& out,bool exclude_alternate_colour,bool double_illuminate,bool no_shadow) const;
192 
194  void write_blender(std::ofstream& out,const std::string& mesh_name,const FloatRGBA* fake_alpha) const;
195 
196  protected:
197 
199  std::vector<Vertex> _vertex;
200 
202  std::vector<Triangle> _triangle;
203 
206 
208  float _emissive;
209 
212 
214  Vertex& vertex(uint i)
215  {
216  return _vertex[i];
217  }
218 
221  {
222  return _triangle[i];
223  }
224 
226  void progress_start(uint steps,const std::string& info) const;
227 
229  void progress_stall(const std::string& reason) const;
230 
232  void progress_step(uint step) const;
233 
235  void progress_complete(const std::string& info) const;
236 
237  private:
238 
240  static ByteRGBA blender_alpha_workround(const ByteRGBA*,const ByteRGBA&);
241 };
242 
244 class TriangleMeshFlat : virtual public TriangleMesh
245 {
246  public:
247 
249  TriangleMeshFlat(ParametersObject::ObjectType obj,float z,uint seed,Progress* progress);
250 
253  {}
254 
256  virtual const Geometry& geometry() const
257  {
258  return _geometry;
259  }
260 
261  private:
262 
264  GeometryFlat _geometry;
265 };
266 
268 class TriangleMeshIcosahedron : virtual public TriangleMesh
269 {
270  public:
271 
273  TriangleMeshIcosahedron(float radius,uint seed,Progress* progress);
274 
277  {}
278 
280  virtual const Geometry& geometry() const
281  {
282  return _geometry;
283  }
284 
285  private:
286 
288  GeometrySpherical _geometry;
289 };
290 
293 {
294  public:
295 
297  TriangleMeshSubdividedIcosahedron(float radius,uint subdivisions,uint flat_subdivisions,uint seed,const XYZ& variation,Progress* progress);
298 
301  {}
302 };
303 
304 #endif