annotate col.c @ 53:a233027e895c

*** empty log message ***
author gongo
date Wed, 08 Nov 2006 07:19:26 +0000
parents 3b71b39411b1
children ce9ef7dcf2f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
1 /*
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
2 * $Id$
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
3 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
4 #include <stdio.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
5 #include"libps2.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
6 #include"ps2util.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
7 #include"col.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
8 #include"mytype.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
9
39
9ec616de33a8 *** empty log message ***
gongo
parents: 20
diff changeset
10 extern FILE *main_fp;
9ec616de33a8 *** empty log message ***
gongo
parents: 20
diff changeset
11
20
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
12 /**
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
13 * v0, v1が描く三角形と点pの内外判定(approx detection)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
14 * 点pはv0, v1が描く面のどこかに位置している。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
15 * pが面の三角形の外側にあるときTRUEを戻し、
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
16 * そうでなければFALSEを戻す。境界は含む。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
17 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
18 static Bool
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
19 col_detect_approx(FVECTOR p, FVECTOR v0, FVECTOR v1)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
20 {
20
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
21 float v_r1, v_r2, r1_r2, r1_r1, r2_r2;
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
22 float a, b, r;
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
23
20
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
24 v_r1 = ps2_vu0_inner_product( p, v0 );
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
25 v_r2 = ps2_vu0_inner_product( p, v1 );
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
26 r1_r2 = ps2_vu0_inner_product( v0, v1 );
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
27 r1_r1 = ps2_vu0_inner_product( v0, v0 );
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
28 r2_r2 = ps2_vu0_inner_product( v1, v1 );
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
29
20
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
30 a = (v_r1 * r2_r2 - v_r2 * r1_r2);
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
31 b = (v_r2 * r1_r1 - v_r1 * r1_r2);
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
32 r = (r1_r1 * r2_r2 - r1_r2 * r1_r2);
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
33
20
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
34 return (a>=0 && b>=0 && a+b-r<=0) ? TRUE : FALSE;
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
35 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
36
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
37 /**
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
38 * 垂心oの算出. 点pからv0, v1が描く面へ垂線を引いたときの交点oを
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
39 * 垂心(orthocenter)と呼ぶ。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
40 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
41 static void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
42 col_orthocenter(FVECTOR o, FVECTOR p, FVECTOR p0, FVECTOR normal)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
43 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
44 FVECTOR o0, d;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
45
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
46 ps2_vu0_sub_vector(o0, p, p0);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
47 o0[W_AXIS] = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
48 ps2_vu0_scale_vector(
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
49 d, normal, ps2_vu0_inner_product(o0, normal));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
50
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
51 o[X_AXIS] = p[X_AXIS] - d[X_AXIS];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
52 o[Y_AXIS] = p[Y_AXIS] - d[Y_AXIS];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
53 o[Z_AXIS] = p[Z_AXIS] - d[Z_AXIS];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
54 o[W_AXIS] = 1;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
55 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
56
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
57 /**
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
58 * 点pと面との衝突(点pとdetection face面との垂心oがdetection face領域の
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
59 * 中にあるかを)判定衝突時(oがdetection face領域の外)はFALSEを戻し、そう
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
60 * でなければTRUE、 垂心(orthocenter)、垂心のある面の法線(normal
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
61 * vector)を戻す.
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
62 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
63 Bool
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
64 col_detect(ColFaceHd* hd, FVECTOR pose, FVECTOR p)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
65 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
66 FVECTOR o, o0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
67 int i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
68 ColFacePtr col_face=hd->colface;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
69 for (i=0; i<hd->facenum; i++) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
70 /*
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
71 * 垂心oを求める
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
72 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
73 col_orthocenter(o, p, col_face[i].p0, col_face[i].normal);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
74 /*
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
75 * detection face上のある点p0から垂心oへのベクトルo0を求める
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
76 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
77 ps2_vu0_sub_vector(o0, o, col_face[i].p0);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
78 /*
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
79 * ベクトルv0, v1の描く三角形とo0とで内外判定(range approx detection)を行う
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
80 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
81 if (col_detect_approx(o0, col_face[i].v0, col_face[i].v1)==TRUE) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
82 goto NOCOLLISION;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
83 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
84 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
85 return FALSE;
20
b1ba4dad7f6e *** empty log message ***
gongo
parents: 0
diff changeset
86 NOCOLLISION:
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
87 ps2_vu0_copy_vector(p, o);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
88 ps2_vu0_copy_vector(pose, col_face[i].normal);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
89 return TRUE;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
90 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
91
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
92 /*
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
93 * detection face作成
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
94 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
95 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
96 col_init(ColFaceHd* hd, PolygonInfo* face)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
97 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
98 int i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
99 FVECTOR p1, p2;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
100 ColFacePtr colface;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
101
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
102 malloc_align16(&hd->free_addr, &colface, sizeof(ColFace)*face->siz/3);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
103
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
104 hd->facenum = face->siz/3;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
105
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
106 for (i=0; i<hd->facenum; i++) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
107 memcpy(colface[i].p0, ((FVECTOR*)face->pnts)[i*3], sizeof(FVECTOR));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
108 memcpy(p1, ((FVECTOR*)face->pnts)[i*3+2], sizeof(FVECTOR));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
109 memcpy(p2, ((FVECTOR*)face->pnts)[i*3+1], sizeof(FVECTOR));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
110
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
111 ps2_vu0_sub_vector(colface[i].v0, p1, colface[i].p0);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
112 ps2_vu0_sub_vector(colface[i].v1, p2, colface[i].p0);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
113
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
114 ps2_vu0_outer_product(colface[i].normal, colface[i].v0, colface[i].v1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
115 ps2_vu0_normalize(colface[i].normal, colface[i].normal);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
116
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
117 if (colface[i].normal[1] > 0) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
118 colface[i].normal[0] = -colface[i].normal[0];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
119 colface[i].normal[1] = -colface[i].normal[1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
120 colface[i].normal[2] = -colface[i].normal[2];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
121 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
122 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
123
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
124 hd->colface = colface;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
125 }