gamnit: rewrite `Mesh::dimensions & center` and move to its own module
[nit.git] / lib / gamnit / depth / model_dimensions.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Dimensions related services for `Model` and `Mesh`
16 module model_dimensions
17
18 import depth_core
19
20 redef class Mesh
21
22 # Dimensions of the bounding box containing all vertices
23 var dimensions = new Point3d[Float](max.x-min.x, max.y-min.y, max.z-min.z) is lazy, writable
24
25 # Center coordinates of all the vertices
26 var center = new Point3d[Float]((min.x+max.x)/2.0, (min.y+max.y)/2.0, (min.z+max.z)/2.0) is lazy, writable
27
28 # Minimum coordinates of all vertices on each axes
29 #
30 # This is a corner of the bounding box.
31 var min: Point3d[Float] is lazy do
32 var mx = inf
33 var my = inf
34 var mz = inf
35 var i = 0
36 while i < vertices.length do
37 mx = mx.min(vertices[i])
38 my = my.min(vertices[i+1])
39 mz = mz.min(vertices[i+2])
40 i += 3
41 end
42 return new Point3d[Float](mx, my, mz)
43 end
44
45 # Maximum coordinates of all vertices on each axes
46 #
47 # This is a corner of the bounding box.
48 var max: Point3d[Float] is lazy do
49 var mx = -inf
50 var my = -inf
51 var mz = -inf
52 var i = 0
53 while i < vertices.length do
54 mx = mx.max(vertices[i])
55 my = my.max(vertices[i+1])
56 mz = mz.max(vertices[i+2])
57 i += 3
58 end
59 return new Point3d[Float](mx, my, mz)
60 end
61 end