RSS

(root)/iphone/common : /source/geometry.m (revision 61)

To get this branch, use:
bzr branch /browse/iphone/common
Line Revision Contents
1 1
//
2
//  geometry.m
3
//
4
//  Created by döme on 26.07.2009.
5
//
6
7 54
/*
8
 * Copyright (c) 2009 Doemoetoer Gulyas.
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. The name of the author may not be used to endorse or promote products
20
 *    derived from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 */
33
34 1
#import "geometry.h"
35
36 2
m16 computeAlignedBasesFromDownVector(v3 acc)
37
{
38
	v3 up = vnormalize(vnegate(acc));
39
	
40 42
	v3 X = {{1.0,0.0,0.0}};
41
	v3 Y = {{0.0,1.0,0.0}};
42
//	v3 Z = {{0.0,0.0,1.0}};
43 2
44
	float udx = vdot(up, X);
45
	float udy = vdot(up, Y);
46
	
47
//	float upalignx = fabs(udx);
48
//	float upaligny = fabs(udy);
49
50
		
51
	
52
	v3 forwardax = vmul(vcross(up,X), 1.0);
53
54
	v3 rightay = vmul(vcross(Y,up), 1.0);
55
	v3 forwarday = vcross(up, rightay);
56
57
	float weightx = 1.0 - udx;
58
	float weighty = 1.0 - udy;
59
60
	v3 forward = vnormalize(vadd(vmul(forwardax, weightx), vmul(forwarday, weighty)));
61
	v3 right = vnormalize(vcross(forward, up));
62
63
	
64 42
	m16 m = {{right.v.x, right.v.y, right.v.z, 0.0f,
65 3
			forward.v.x, forward.v.y, forward.v.z, 0.0f,
66
			up.v.x, up.v.y, up.v.z, 0.0f,
67 42
			0.0f, 0.0f, 0.0f, 1.0f}};
68 3
	
69
	return m;
70
}
71
72 6
m16 computeAlignedBasesFromDownVector2(v3 acc, v3 pf)
73 3
{
74 6
	if (vdot(acc,acc) < FLT_EPSILON)
75
		acc = vcreate(0.0,0.0,-1.0);
76
	if (vdot(pf,pf) < FLT_EPSILON)
77
		pf = vcreate(0.0,1.0,0.0);
78 3
	v3 up = vnormalize(vnegate(acc));
79
	
80
	v3 right = vnormalize(vcross(pf, up));
81
	v3 forward = vnormalize(vcross(up,right));
82
83
	
84 42
	m16 m = {{right.v.x, right.v.y, right.v.z, 0.0f,
85 3
			forward.v.x, forward.v.y, forward.v.z, 0.0f,
86
			up.v.x, up.v.y, up.v.z, 0.0f,
87 42
			0.0f, 0.0f, 0.0f, 1.0f}};
88 2
	
89
	return m;
90
}
91
92 15
93
CGPoint CGPointTransform(CGPoint a, CGAffineTransform m)
94
{
95
	CGPoint b;
96
	b.x = a.x*m.a + a.y*m.c + m.tx;
97
	b.y = a.x*m.b + a.y*m.d + m.ty;
98
	return b;
99
}
100
101
102
struct _bezierTransformerRec
103
{
104
	CGMutablePathRef		path;
105
	const CGAffineTransform	m;
106
};
107
108
static void _pathTransformer(void *_info,
109
    const CGPathElement *element)
110
{
111
	struct _bezierTransformerRec* info = _info;
112
	CGMutablePathRef path = info->path;
113
114
	switch(element->type)
115
	{
116
		case kCGPathElementMoveToPoint:
117
			CGPathMoveToPoint(path, &info->m, element->points[0].x, element->points[0].y);
118
			break;
119
		case kCGPathElementAddLineToPoint:
120
		{
121
			CGPathAddLineToPoint(path, &info->m, element->points[0].x, element->points[0].y);
122
			break;
123
		}
124
		case kCGPathElementAddQuadCurveToPoint:
125
		{
126
			CGPathAddQuadCurveToPoint(path, &info->m, element->points[0].x, element->points[0].y, element->points[1].x, element->points[1].y);
127
			break;
128
		}
129
		case kCGPathElementAddCurveToPoint:
130
		{
131
			CGPathAddCurveToPoint(path, &info->m, element->points[0].x, element->points[0].y, element->points[1].x, element->points[1].y, element->points[2].x, element->points[2].y);			
132
			break;
133
		}
134
		case kCGPathElementCloseSubpath:
135
		{
136
			CGPathCloseSubpath(path);
137
			break;
138
		}
139
	};
140
}
141
142
static void _pathTransformerM(void *_info,
143
    const CGPathElement *element)
144
{
145
	struct _bezierTransformerRec* info = _info;
146
	CGMutablePathRef path = info->path;
147
148
	switch(element->type)
149
	{
150
		case kCGPathElementMoveToPoint:
151
		{
152
			CGPoint c0 = CGPointTransform(element->points[0], info->m);
153
			CGPathMoveToPoint(path, NULL, c0.x, c0.y);
154
			break;
155
		}
156
		case kCGPathElementAddLineToPoint:
157
		{
158
			CGPoint c0 = CGPointTransform(element->points[0], info->m);
159
			CGPathAddLineToPoint(path, NULL, c0.x, c0.y);
160
			break;
161
		}
162
		case kCGPathElementAddQuadCurveToPoint:
163
		{
164
			CGPoint c0 = CGPointTransform(element->points[0], info->m);
165
			CGPoint c1 = CGPointTransform(element->points[1], info->m);
166
			CGPathAddQuadCurveToPoint(path, NULL, c0.x, c0.y, c1.x, c1.y);
167
			break;
168
		}
169
		case kCGPathElementAddCurveToPoint:
170
		{
171
			CGPoint c0 = CGPointTransform(element->points[0], info->m);
172
			CGPoint c1 = CGPointTransform(element->points[1], info->m);
173
			CGPoint c2 = CGPointTransform(element->points[2], info->m);
174
			CGPathAddCurveToPoint(path, NULL, c0.x, c0.y, c1.x, c1.y, c2.x, c2.y);			
175
			break;
176
		}
177
		case kCGPathElementCloseSubpath:
178
		{
179
			CGPathCloseSubpath(path);
180
			break;
181
		}
182
	};
183
}
184
185
186
CGPathRef CreateTransformedPathFromCGPath(CGPathRef cpath, CGAffineTransform m)
187
{
188
	struct _bezierTransformerRec rec = {CGPathCreateMutable(), m};
189
	CGPathApply(cpath, &rec, _pathTransformerM);
190
	
191
	return rec.path;
192
}

Loggerhead 1.17 is a web-based interface for Bazaar branches