Lightmetrica  0.0.1.50dbee3 (yosakoi)
 All Classes Functions Variables Typedefs Enumerations Enumerator
stub.trianglemesh.h
1 /*
2  Lightmetrica : A research-oriented renderer
3 
4  Copyright (c) 2014 Hisanari Otsu
5 
6  This program 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  This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 #ifndef LIB_LIGHTMETRICA_TEST_STUB_TRIANGLE_MESH_H
22 #define LIB_LIGHTMETRICA_TEST_STUB_TRIANGLE_MESH_H
23 
24 #include "common.h"
25 #include <lightmetrica/trianglemesh.h>
26 #include <lightmetrica/align.h>
27 #include <random>
28 
29 LM_NAMESPACE_BEGIN
30 LM_TEST_NAMESPACE_BEGIN
31 
33 {
34 public:
35 
36  LM_COMPONENT_IMPL_DEF("stub");
37 
38 public:
39 
40  virtual int NumVertices() const { return static_cast<int>(positions.size()); }
41  virtual int NumFaces() const { return static_cast<int>(faces.size()); }
42  virtual const Math::Float* Positions() const { return positions.empty() ? nullptr : &positions[0]; }
43  virtual const Math::Float* Normals() const { return normals.empty() ? nullptr : &normals[0]; }
44  virtual const Math::Float* TexCoords() const { return texcoords.empty() ? nullptr : &texcoords[0]; }
45  virtual const unsigned int* Faces() const { return faces.empty() ? nullptr : &faces[0]; }
46  virtual bool Load( const ConfigNode& node, const Assets& assets ) { return false; }
47 
48 protected:
49 
50  std::vector<Math::Float> positions;
51  std::vector<Math::Float> normals;
52  std::vector<Math::Float> texcoords;
53  std::vector<unsigned int> faces;
54 
55 };
56 
57 // {(x, y, z) : 0<=x,y<=1, z=0,-1}
59 {
60 public:
61 
63  {
64  const double ps[] =
65  {
66  0, 0, 0,
67  1, 0, 0,
68  1, 1, 0,
69  0, 1, 0,
70  0, 0, -1,
71  1, 0, -1,
72  1, 1, -1,
73  0, 1, -1
74  };
75 
76  const double ns[] =
77  {
78  0, 0, 1,
79  0, 0, 1,
80  0, 0, 1,
81  0, 0, 1,
82  0, 0, 1,
83  0, 0, 1,
84  0, 0, 1,
85  0, 0, 1
86  };
87 
88  const double ts[] =
89  {
90  0, 0,
91  1, 0,
92  1, 1,
93  0, 1,
94  0, 0,
95  1, 0,
96  1, 1,
97  0, 1
98  };
99 
100  const unsigned int fs[] =
101  {
102  0, 1, 2,
103  0, 2, 3,
104  4, 5, 6,
105  4, 6, 7
106  };
107 
108  for (int i = 0; i < 8; i++)
109  {
110  positions.push_back(Math::Float(ps[3*i ]));
111  positions.push_back(Math::Float(ps[3*i+1]));
112  positions.push_back(Math::Float(ps[3*i+2]));
113  normals.push_back(Math::Float(ns[3*i ]));
114  normals.push_back(Math::Float(ns[3*i+1]));
115  normals.push_back(Math::Float(ns[3*i+2]));
116  texcoords.push_back(Math::Float(ts[2*i ]));
117  texcoords.push_back(Math::Float(ts[2*i+1]));
118  }
119 
120  for (int i = 0; i < 4; i++)
121  {
122  faces.push_back(fs[3*i]);
123  faces.push_back(fs[3*i+1]);
124  faces.push_back(fs[3*i+2]);
125  }
126  }
127 
128 };
129 
130 // {(x, y, z) : 0<=x,y<=1, x=-z}
132 {
133 public:
134 
136  {
137  const double ps[] =
138  {
139  0, 0, 0,
140  1, 0, -1,
141  1, 1, -1,
142  0, 1, 0
143  };
144 
145  const double ts[] =
146  {
147  0, 0,
148  1, 0,
149  1, 1,
150  0, 1
151  };
152 
153  const unsigned int fs[] =
154  {
155  0, 1, 2,
156  0, 2, 3
157  };
158 
159  auto n = Math::Normalize(Math::Vec3(1, 0, 1));
160 
161  for (int i = 0; i < 4; i++)
162  {
163  positions.push_back(Math::Float(ps[3*i ]));
164  positions.push_back(Math::Float(ps[3*i+1]));
165  positions.push_back(Math::Float(ps[3*i+2]));
166  normals.push_back(n[0]);
167  normals.push_back(n[1]);
168  normals.push_back(n[2]);
169  texcoords.push_back(Math::Float(ts[2*i ]));
170  texcoords.push_back(Math::Float(ts[2*i+1]));
171  }
172 
173  for (int i = 0; i < 2; i++)
174  {
175  faces.push_back(fs[3*i]);
176  faces.push_back(fs[3*i+1]);
177  faces.push_back(fs[3*i+2]);
178  }
179  }
180 
181 };
182 
183 // Many triangles in [0, 1]^3
185 {
186 public:
187 
189  {
190  // Fix seed
191  std::mt19937 gen(42);
192  std::uniform_real_distribution<double> dist;
193 
194  const int FaceCount = 1000;
195  for (int i = 0; i < FaceCount; i++)
196  {
197  auto p1 = Math::Vec3(Math::Float(dist(gen)), Math::Float(dist(gen)), Math::Float(dist(gen)));
198  auto p2 = Math::Vec3(Math::Float(dist(gen)), Math::Float(dist(gen)), Math::Float(dist(gen)));
199  auto p3 = Math::Vec3(Math::Float(dist(gen)), Math::Float(dist(gen)), Math::Float(dist(gen)));
200 
201  positions.push_back(p1[0]);
202  positions.push_back(p1[1]);
203  positions.push_back(p1[2]);
204  positions.push_back(p2[0]);
205  positions.push_back(p2[1]);
206  positions.push_back(p2[2]);
207  positions.push_back(p3[0]);
208  positions.push_back(p3[1]);
209  positions.push_back(p3[2]);
210 
211  auto n = Math::Cross(p2 - p1, p3 - p1);
212  for (int j = 0; j < 3; j++)
213  {
214  normals.push_back(n[0]);
215  normals.push_back(n[1]);
216  normals.push_back(n[2]);
217  }
218 
219  faces.push_back(3*i);
220  faces.push_back(3*i+1);
221  faces.push_back(3*i+2);
222  }
223  }
224 
225 };
226 
227 LM_TEST_NAMESPACE_END
228 LM_NAMESPACE_END
229 
230 #endif // LIB_LIGHTMETRICA_TEST_STUB_TRIANGLE_MESH_H
virtual const Math::Float * Normals() const
Definition: stub.trianglemesh.h:43
virtual const Math::Float * Positions() const
Definition: stub.trianglemesh.h:42
virtual int NumVertices() const
Definition: stub.trianglemesh.h:40
Definition: assets.h:39
Definition: stub.trianglemesh.h:32
virtual const Math::Float * TexCoords() const
Definition: stub.trianglemesh.h:44
virtual bool Load(const ConfigNode &node, const Assets &assets)
Definition: stub.trianglemesh.h:46
Definition: stub.trianglemesh.h:58
virtual int NumFaces() const
Definition: stub.trianglemesh.h:41
Definition: confignode.h:37
Definition: stub.trianglemesh.h:184
Definition: trianglemesh.h:34
virtual const unsigned int * Faces() const
Definition: stub.trianglemesh.h:45
Definition: stub.trianglemesh.h:131