Subversion Repositories gelsvn

Rev

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

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