Subversion Repositories gelsvn

Rev

Rev 375 | Rev 541 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
78 jab 1
// bdl, jab, feb 2005
2
// Inspired by Nate Robins Obj loader
3
 
4
#include <CGLA/Vec3f.h>
5
#include <stdio.h>
6
#include <iostream>
7
 
178 bj 8
#include "TriMesh.h"
9
 
78 jab 10
using namespace std;
11
using namespace CGLA;
12
 
79 jab 13
namespace Geometry 
78 jab 14
{
15
	int TriMesh::find_material(const string& name) const
16
	{
136 jab 17
		for(size_t i=0;i<materials.size(); ++i)
78 jab 18
			{
19
				if(materials[i].name == name)
20
					return i;
21
			}
22
		return 0;
23
	}
178 bj 24
 
78 jab 25
	void TriMesh::compute_normals()
26
	{		
27
		// By default the normal faces are the same as the geometry faces
28
		// and there are just as many normals as vertices, so we simply
29
		// copy.
30
		normals = geometry;
31
 
32
		const int NV = normals.no_vertices();
33
		// The normals are initialized to zero.
34
		int i;
35
		for(i=0;i<NV; ++i)
36
			normals.vertex_rw(i) = Vec3f(0);
37
 
38
		// For each face
39
		int NF = geometry.no_faces();
40
		for(i=0;i<NF; ++i)
41
      {
42
				// Compute the normal
371 jab 43
				const Vec3i f  = geometry.face(i);
78 jab 44
				const Vec3f p0 = geometry.vertex(f[0]);
45
				const Vec3f a  = geometry.vertex(f[1]) - p0;
46
				const Vec3f b  = geometry.vertex(f[2]) - p0;
47
				Vec3f face_normal = cross(a,b);
48
				float l = sqr_length(face_normal);
49
				if(l > 0.0f)
50
					face_normal /= sqrt(l);
51
 
52
				// Add the angle weighted normal to each vertex
53
				for(int j=0;j<3; ++j)
54
					{
55
						const Vec3f p0 = geometry.vertex(f[j]);
295 jrf 56
						Vec3f a = geometry.vertex(f[(j+1)%3]) - p0;
57
						float l_a = sqr_length(a);
58
						if(l_a > 0.0f)
59
							a /= sqrt(l_a);
60
						Vec3f b = geometry.vertex(f[(j+2)%3]) - p0;
61
						float l_b = sqr_length(b);
62
						if(l_b > 0.0f)
63
							b /= sqrt(l_b);
78 jab 64
						float d = max(-1.0f, min(1.0f, dot(a,b)));
65
						normals.vertex_rw(f[j]) += face_normal * acos(d);
66
					}
67
      }
68
 
69
		// Normalize all normals
70
    for(i=0;i<NV; ++i)
71
			{
295 jrf 72
				float l_vert_rw = sqr_length(normals.vertex_rw(i));
73
				if(l_vert_rw > 0.0f)
334 jab 74
					normals.vertex_rw(i) /= sqrt(l_vert_rw);
78 jab 75
			}
76
	}
77
 
78
 
375 jrf 79
  bool TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
78 jab 80
  {
375 jrf 81
    if(geometry.no_vertices() == 0)
82
      return false;
83
 
78 jab 84
    int i;
85
    p0 = geometry.vertex(0);
86
    p7 = geometry.vertex(0);
87
    for(i=1;i<geometry.no_vertices();i++) 
88
      {
89
				p0 = v_min(geometry.vertex(i), p0);
90
				p7 = v_max(geometry.vertex(i), p7);
91
      }
375 jrf 92
    return true;
78 jab 93
  }
94
 
375 jrf 95
  bool TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
78 jab 96
  {
97
    Vec3f p0,p7;
375 jrf 98
    if(!get_bbox(p0, p7))
99
      return false;
100
 
78 jab 101
    Vec3f rad = (p7 - p0)/2.0;
102
    c = p0 + rad;
103
    r = rad.length();
375 jrf 104
    return true;
78 jab 105
  }
106
 
419 jrf 107
  void TriMesh::transform(CGLA::Mat4x4f m)
108
  {
109
    for(int i = 0; i < geometry.no_vertices(); ++i)
110
      geometry.vertex_rw(i) = m.mul_3D_point(geometry.vertex(i));
111
    for(int i = 0; i < normals.no_vertices(); ++i)
112
      normals.vertex_rw(i) = m.mul_3D_vector(normals.vertex(i));
113
  }
78 jab 114
}