Subversion Repositories gelsvn

Rev

Rev 567 | Rev 601 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 567 Rev 595
-
 
1
/* ----------------------------------------------------------------------- *
1
// bdl, jab, feb 2005
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
2
// Inspired by Nate Robins Obj loader
3
 * Copyright (C) the authors and DTU Informatics
-
 
4
 * For license and list of authors, see ../../doc/intro.pdf
-
 
5
 * ----------------------------------------------------------------------- */
3
 
6
 
4
#include <CGLA/Vec3f.h>
7
#include <CGLA/Vec3f.h>
5
#include <stdio.h>
8
#include <stdio.h>
6
#include <iostream>
9
#include <iostream>
7
 
10
 
8
#include "TriMesh.h"
11
#include "TriMesh.h"
9
 
12
 
10
using namespace std;
13
using namespace std;
11
using namespace CGLA;
14
using namespace CGLA;
12
 
15
 
13
namespace Geometry 
16
namespace Geometry 
14
{
17
{
15
	int TriMesh::find_material(const string& name) const
18
	int TriMesh::find_material(const string& name) const
16
	{
19
	{
17
		for(size_t i=0;i<materials.size(); ++i)
20
		for(size_t i=0;i<materials.size(); ++i)
18
			{
21
			{
19
				if(materials[i].name == name)
22
				if(materials[i].name == name)
20
					return i;
23
                    return i;
21
			}
24
            }
22
		return 0;
25
		return 0;
23
	}
26
	}
24
	
27
	
25
	void TriMesh::compute_normals()
28
	void TriMesh::compute_normals()
26
	{		
29
	{		
27
		// By default the normal faces are the same as the geometry faces
30
		// 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
31
		// and there are just as many normals as vertices, so we simply
29
		// copy.
32
		// copy.
30
		normals = geometry;
33
		normals = geometry;
31
 
34
 
32
		const int NV = normals.no_vertices();
35
		const int NV = normals.no_vertices();
33
		// The normals are initialized to zero.
36
		// The normals are initialized to zero.
34
		int i;
37
		int i;
35
		for(i=0;i<NV; ++i)
38
		for(i=0;i<NV; ++i)
36
			normals.vertex_rw(i) = Vec3f(0);
39
			normals.vertex_rw(i) = Vec3f(0);
37
 
40
 
38
		// For each face
41
		// For each face
39
		int NF = geometry.no_faces();
42
		int NF = geometry.no_faces();
40
		for(i=0;i<NF; ++i)
43
		for(i=0;i<NF; ++i)
41
      {
44
      {
42
				// Compute the normal
45
				// Compute the normal
43
				const Vec3i& f  = geometry.face(i);
46
				const Vec3i& f  = geometry.face(i);
44
				const Vec3f& p0 = geometry.vertex(f[0]);
47
				const Vec3f& p0 = geometry.vertex(f[0]);
45
				const Vec3f& a  = geometry.vertex(f[1]) - p0;
48
				const Vec3f& a  = geometry.vertex(f[1]) - p0;
46
				const Vec3f& b  = geometry.vertex(f[2]) - p0;
49
				const Vec3f& b  = geometry.vertex(f[2]) - p0;
47
				Vec3f face_normal = cross(a,b);
50
				Vec3f face_normal = cross(a,b);
48
				float l = sqr_length(face_normal);
51
				float l = sqr_length(face_normal);
49
				if(l > 0.0f)
52
				if(l > 0.0f)
50
					face_normal /= sqrt(l);
53
					face_normal /= sqrt(l);
51
				
54
				
52
				// Add the angle weighted normal to each vertex
55
				// Add the angle weighted normal to each vertex
53
				for(int j=0;j<3; ++j)
56
				for(int j=0;j<3; ++j)
54
					{
57
					{
55
						const Vec3f& p0 = geometry.vertex(f[j]);
58
						const Vec3f& p0 = geometry.vertex(f[j]);
56
						Vec3f a = geometry.vertex(f[(j+1)%3]) - p0;
59
						Vec3f a = geometry.vertex(f[(j+1)%3]) - p0;
57
						float l_a = sqr_length(a);
60
						float l_a = sqr_length(a);
58
						if(l_a > 0.0f)
61
						if(l_a > 0.0f)
59
							a /= sqrt(l_a);
62
							a /= sqrt(l_a);
60
						Vec3f b = geometry.vertex(f[(j+2)%3]) - p0;
63
						Vec3f b = geometry.vertex(f[(j+2)%3]) - p0;
61
						float l_b = sqr_length(b);
64
						float l_b = sqr_length(b);
62
						if(l_b > 0.0f)
65
						if(l_b > 0.0f)
63
							b /= sqrt(l_b);
66
							b /= sqrt(l_b);
64
						float d = max(-1.0f, min(1.0f, dot(a,b)));
67
						float d = max(-1.0f, min(1.0f, dot(a,b)));
65
						normals.vertex_rw(f[j]) += face_normal * acos(d);
68
						normals.vertex_rw(f[j]) += face_normal * acos(d);
66
					}
69
					}
67
      }
70
      }
68
 
71
 
69
		// Normalize all normals
72
		// Normalize all normals
70
    for(i=0;i<NV; ++i)
73
    for(i=0;i<NV; ++i)
71
			{
74
			{
72
				float l_vert_rw = sqr_length(normals.vertex_rw(i));
75
				float l_vert_rw = sqr_length(normals.vertex_rw(i));
73
				if(l_vert_rw > 0.0f)
76
				if(l_vert_rw > 0.0f)
74
					normals.vertex_rw(i) /= sqrt(l_vert_rw);
77
					normals.vertex_rw(i) /= sqrt(l_vert_rw);
75
			}
78
			}
76
	}
79
	}
77
 
80
 
78
	void TriMesh::compute_areas()
81
	void TriMesh::compute_areas()
79
	{
82
	{
80
    int no_of_faces = geometry.no_faces();
83
    int no_of_faces = geometry.no_faces();
81
    surface_area = 0.0f;
84
    surface_area = 0.0f;
82
    face_areas.resize(no_of_faces);
85
    face_areas.resize(no_of_faces);
83
    face_area_cdf.resize(no_of_faces);
86
    face_area_cdf.resize(no_of_faces);
84
		for(int i = 0; i < no_of_faces; ++i)
87
		for(int i = 0; i < no_of_faces; ++i)
85
    {
88
    {
86
			const Vec3i& f  = geometry.face(i);
89
			const Vec3i& f  = geometry.face(i);
87
			const Vec3f& p0 = geometry.vertex(f[0]);
90
			const Vec3f& p0 = geometry.vertex(f[0]);
88
			const Vec3f& a  = geometry.vertex(f[1]) - p0;
91
			const Vec3f& a  = geometry.vertex(f[1]) - p0;
89
			const Vec3f& b  = geometry.vertex(f[2]) - p0;
92
			const Vec3f& b  = geometry.vertex(f[2]) - p0;
90
      face_areas[i] = 0.5f*cross(a, b).length();
93
      face_areas[i] = 0.5f*cross(a, b).length();
91
      face_area_cdf[i] = surface_area + face_areas[i];
94
      face_area_cdf[i] = surface_area + face_areas[i];
92
      surface_area += face_areas[i];
95
      surface_area += face_areas[i];
93
    }
96
    }
94
    if(surface_area > 0.0f)
97
    if(surface_area > 0.0f)
95
      for(int i = 0; i < no_of_faces; ++i)
98
      for(int i = 0; i < no_of_faces; ++i)
96
        face_area_cdf[i] /= surface_area;
99
        face_area_cdf[i] /= surface_area;
97
	}
100
	}
98
 
101
 
99
  bool TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
102
  bool TriMesh::get_bbox(CGLA::Vec3f& p0, CGLA::Vec3f& p7) const
100
  {
103
  {
101
    if(geometry.no_vertices() == 0)
104
    if(geometry.no_vertices() == 0)
102
      return false;
105
      return false;
103
 
106
 
104
    int i;
107
    int i;
105
    p0 = geometry.vertex(0);
108
    p0 = geometry.vertex(0);
106
    p7 = geometry.vertex(0);
109
    p7 = geometry.vertex(0);
107
    for(i=1;i<geometry.no_vertices();i++) 
110
    for(i=1;i<geometry.no_vertices();i++) 
108
      {
111
      {
109
				p0 = v_min(geometry.vertex(i), p0);
112
				p0 = v_min(geometry.vertex(i), p0);
110
				p7 = v_max(geometry.vertex(i), p7);
113
				p7 = v_max(geometry.vertex(i), p7);
111
      }
114
      }
112
    return true;
115
    return true;
113
  }
116
  }
114
 
117
 
115
  bool TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
118
  bool TriMesh::get_bsphere(CGLA::Vec3f& c, float& r) const
116
  {
119
  {
117
    Vec3f p0,p7;
120
    Vec3f p0,p7;
118
    if(!get_bbox(p0, p7))
121
    if(!get_bbox(p0, p7))
119
      return false;
122
      return false;
120
 
123
 
121
    Vec3f rad = (p7 - p0)/2.0;
124
    Vec3f rad = (p7 - p0)/2.0;
122
    c = p0 + rad;
125
    c = p0 + rad;
123
    r = rad.length();
126
    r = rad.length();
124
    return true;
127
    return true;
125
  }
128
  }
126
 
129
 
127
  void TriMesh::transform(CGLA::Mat4x4f m)
130
  void TriMesh::transform(CGLA::Mat4x4f m)
128
  {
131
  {
129
    for(int i = 0; i < geometry.no_vertices(); ++i)
132
    for(int i = 0; i < geometry.no_vertices(); ++i)
130
      geometry.vertex_rw(i) = m.mul_3D_point(geometry.vertex(i));
133
      geometry.vertex_rw(i) = m.mul_3D_point(geometry.vertex(i));
131
    for(int i = 0; i < normals.no_vertices(); ++i)
134
    for(int i = 0; i < normals.no_vertices(); ++i)
132
      normals.vertex_rw(i) = normalize(m.mul_3D_vector(normals.vertex(i)));
135
      normals.vertex_rw(i) = normalize(m.mul_3D_vector(normals.vertex(i)));
133
  }
136
  }
134
}
137
}
135
 
138