#
root/OpenSceneGraph/trunk/src/osgPlugins/3ds/lib3ds/lib3ds_vector.c
@
10853

Revision 10853, 5.1 kB (checked in by robert, 5 years ago) |
---|

Rev | Line | |
---|---|---|

[10853] | 1 | /* |

2 | Copyright (C) 1996-2008 by Jan Eric Kyprianidis <www.kyprianidis.com> | |

3 | All rights reserved. | |

4 | | |

5 | This program is free software: you can redistribute it and/or modify | |

6 | it under the terms of the GNU Lesser General Public License as published | |

7 | by the Free Software Foundation, either version 2.1 of the License, or | |

8 | (at your option) any later version. | |

9 | ||

10 | Thisprogram is distributed in the hope that it will be useful, | |

11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |

12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |

13 | GNU Lesser General Public License for more details. | |

14 | | |

15 | You should have received a copy of the GNU Lesser General Public License | |

16 | along with this program; If not, see <http://www.gnu.org/licenses/>. | |

17 | */ | |

18 | #include "lib3ds_impl.h" | |

19 | ||

20 | ||

21 | void | |

22 | lib3ds_vector_make(float c[3], float x, float y, float z) { | |

23 | c[0] = x; | |

24 | c[1] = y; | |

25 | c[2] = z; | |

26 | } | |

27 | ||

28 | ||

29 | void | |

30 | lib3ds_vector_zero(float c[3]) { | |

31 | int i; | |

32 | for (i = 0; i < 3; ++i) { | |

33 | c[i] = 0.0f; | |

34 | } | |

35 | } | |

36 | ||

37 | ||

38 | void | |

39 | lib3ds_vector_copy(float dst[3], float src[3]) { | |

40 | int i; | |

41 | for (i = 0; i < 3; ++i) { | |

42 | dst[i] = src[i]; | |

43 | } | |

44 | } | |

45 | ||

46 | ||

47 | /*! | |

48 | * Add two vectors. | |

49 | * | |

50 | * \param c Result. | |

51 | * \param a First addend. | |

52 | * \param b Second addend. | |

53 | */ | |

54 | void | |

55 | lib3ds_vector_add(float c[3], float a[3], float b[3]) { | |

56 | int i; | |

57 | for (i = 0; i < 3; ++i) { | |

58 | c[i] = a[i] + b[i]; | |

59 | } | |

60 | } | |

61 | ||

62 | ||

63 | /*! | |

64 | * Subtract two vectors. | |

65 | * | |

66 | * \param c Result. | |

67 | * \param a Addend. | |

68 | * \param b Minuend. | |

69 | */ | |

70 | void | |

71 | lib3ds_vector_sub(float c[3], float a[3], float b[3]) { | |

72 | int i; | |

73 | for (i = 0; i < 3; ++i) { | |

74 | c[i] = a[i] - b[i]; | |

75 | } | |

76 | } | |

77 | ||

78 | ||

79 | /*! | |

80 | * Multiply a vector by a scalar. | |

81 | * | |

82 | * \param c Vector to be multiplied. | |

83 | * \param k Scalar. | |

84 | */ | |

85 | void | |

86 | lib3ds_vector_scalar_mul(float c[3], float a[3], float k) { | |

87 | int i; | |

88 | for (i = 0; i < 3; ++i) { | |

89 | c[i] = a[i] * k; | |

90 | } | |

91 | } | |

92 | ||

93 | ||

94 | /*! | |

95 | * Compute cross product. | |

96 | * | |

97 | * \param c Result. | |

98 | * \param a First vector. | |

99 | * \param b Second vector. | |

100 | */ | |

101 | void | |

102 | lib3ds_vector_cross(float c[3], float a[3], float b[3]) { | |

103 | c[0] = a[1] * b[2] - a[2] * b[1]; | |

104 | c[1] = a[2] * b[0] - a[0] * b[2]; | |

105 | c[2] = a[0] * b[1] - a[1] * b[0]; | |

106 | } | |

107 | ||

108 | ||

109 | /*! | |

110 | * Compute dot product. | |

111 | * | |

112 | * \param a First vector. | |

113 | * \param b Second vector. | |

114 | * | |

115 | * \return Dot product. | |

116 | */ | |

117 | float | |

118 | lib3ds_vector_dot(float a[3], float b[3]) { | |

119 | return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]); | |

120 | } | |

121 | ||

122 | ||

123 | /*! | |

124 | * Compute length of vector. | |

125 | * | |

126 | * Computes |c| = sqrt(x*x + y*y + z*z) | |

127 | * | |

128 | * \param c Vector to compute. | |

129 | * | |

130 | * \return Length of vector. | |

131 | */ | |

132 | float | |

133 | lib3ds_vector_length(float c[3]) { | |

134 | return((float)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2])); | |

135 | } | |

136 | ||

137 | ||

138 | /*! | |

139 | * Normalize a vector. | |

140 | * | |

141 | * Scales a vector so that its length is 1.0. | |

142 | * | |

143 | * \param c Vector to normalize. | |

144 | */ | |

145 | void | |

146 | lib3ds_vector_normalize(float c[3]) { | |

147 | float l, m; | |

148 | ||

149 | l = (float)sqrt(c[0] * c[0] + c[1] * c[1] + c[2] * c[2]); | |

150 | if (fabs(l) < LIB3DS_EPSILON) { | |

151 | if ((c[0] >= c[1]) && (c[0] >= c[2])) { | |

152 | c[0] = 1.0f; | |

153 | c[1] = c[2] = 0.0f; | |

154 | } else | |

155 | if (c[1] >= c[2]) { | |

156 | c[1] = 1.0f; | |

157 | c[0] = c[2] = 0.0f; | |

158 | } else { | |

159 | c[2] = 1.0f; | |

160 | c[0] = c[1] = 0.0f; | |

161 | } | |

162 | } else { | |

163 | m = 1.0f / l; | |

164 | c[0] *= m; | |

165 | c[1] *= m; | |

166 | c[2] *= m; | |

167 | } | |

168 | } | |

169 | ||

170 | ||

171 | /*! | |

172 | * Compute a vector normal to two line segments. | |

173 | * | |

174 | * Computes the normal vector to the lines b-a and b-c. | |

175 | * | |

176 | * \param n Returned normal vector. | |

177 | * \param a Endpoint of first line. | |

178 | * \param b Base point of both lines. | |

179 | * \param c Endpoint of second line. | |

180 | */ | |

181 | void | |

182 | lib3ds_vector_normal(float n[3], float a[3], float b[3], float c[3]) { | |

183 | float p[3], q[3]; | |

184 | ||

185 | lib3ds_vector_sub(p, c, b); | |

186 | lib3ds_vector_sub(q, a, b); | |

187 | lib3ds_vector_cross(n, p, q); | |

188 | lib3ds_vector_normalize(n); | |

189 | } | |

190 | ||

191 | ||

192 | /*! | |

193 | * Multiply a point by a transformation matrix. | |

194 | * | |

195 | * Applies the given transformation matrix to the given point. With some | |

196 | * transformation matrices, a vector may also be transformed. | |

197 | * | |

198 | * \param c Result. | |

199 | * \param m Transformation matrix. | |

200 | * \param a Input point. | |

201 | */ | |

202 | void | |

203 | lib3ds_vector_transform(float c[3], float m[4][4], float a[3]) { | |

204 | c[0] = m[0][0] * a[0] + m[1][0] * a[1] + m[2][0] * a[2] + m[3][0]; | |

205 | c[1] = m[0][1] * a[0] + m[1][1] * a[1] + m[2][1] * a[2] + m[3][1]; | |

206 | c[2] = m[0][2] * a[0] + m[1][2] * a[1] + m[2][2] * a[2] + m[3][2]; | |

207 | } | |

208 | ||

209 | ||

210 | /*! | |

211 | * c[i] = min(c[i], a[i]); | |

212 | * | |

213 | * Computes minimum values of x,y,z independently. | |

214 | */ | |

215 | void | |

216 | lib3ds_vector_min(float c[3], float a[3]) { | |

217 | int i; | |

218 | for (i = 0; i < 3; ++i) { | |

219 | if (a[i] < c[i]) { | |

220 | c[i] = a[i]; | |

221 | } | |

222 | } | |

223 | } | |

224 | ||

225 | ||

226 | /*! | |

227 | * c[i] = max(c[i], a[i]); | |

228 | * | |

229 | * Computes maximum values of x,y,z independently. | |

230 | */ | |

231 | void | |

232 | lib3ds_vector_max(float c[3], float a[3]) { | |

233 | int i; | |

234 | for (i = 0; i < 3; ++i) { | |

235 | if (a[i] > c[i]) { | |

236 | c[i] = a[i]; | |

237 | } | |

238 | } | |

239 | } | |

240 | ||

241 | ||

242 | void | |

243 | lib3ds_vector_dump(float c[3]) { | |

244 | fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]); | |

245 | } |

**Note:**See TracBrowser for help on using the browser.