diff options
Diffstat (limited to 'OpenSim/Framework/PhysicsInertia.cs')
-rw-r--r-- | OpenSim/Framework/PhysicsInertia.cs | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/OpenSim/Framework/PhysicsInertia.cs b/OpenSim/Framework/PhysicsInertia.cs new file mode 100644 index 0000000..af70634 --- /dev/null +++ b/OpenSim/Framework/PhysicsInertia.cs | |||
@@ -0,0 +1,262 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using OpenMetaverse; | ||
31 | using System.Text; | ||
32 | using System.IO; | ||
33 | using System.Xml; | ||
34 | |||
35 | namespace OpenSim.Framework | ||
36 | { | ||
37 | public class PhysicsInertiaData | ||
38 | { | ||
39 | public float TotalMass; // the total mass of a linkset | ||
40 | public Vector3 CenterOfMass; // the center of mass position relative to root part position | ||
41 | public Vector3 Inertia; // (Ixx, Iyy, Izz) moment of inertia relative to center of mass and principal axis in local coords | ||
42 | public Vector4 InertiaRotation; // if principal axis don't match local axis, the principal axis rotation | ||
43 | // or the upper triangle of the inertia tensor | ||
44 | // Ixy (= Iyx), Ixz (= Izx), Iyz (= Izy)) | ||
45 | |||
46 | public PhysicsInertiaData() | ||
47 | { | ||
48 | } | ||
49 | |||
50 | public PhysicsInertiaData(PhysicsInertiaData source) | ||
51 | { | ||
52 | TotalMass = source.TotalMass; | ||
53 | CenterOfMass = source.CenterOfMass; | ||
54 | Inertia = source.Inertia; | ||
55 | InertiaRotation = source.InertiaRotation; | ||
56 | } | ||
57 | |||
58 | private XmlTextWriter writer; | ||
59 | |||
60 | private void XWint(string name, int i) | ||
61 | { | ||
62 | writer.WriteElementString(name, i.ToString()); | ||
63 | } | ||
64 | |||
65 | private void XWfloat(string name, float f) | ||
66 | { | ||
67 | writer.WriteElementString(name, f.ToString(Utils.EnUsCulture)); | ||
68 | } | ||
69 | |||
70 | private void XWVector(string name, Vector3 vec) | ||
71 | { | ||
72 | writer.WriteStartElement(name); | ||
73 | writer.WriteElementString("X", vec.X.ToString(Utils.EnUsCulture)); | ||
74 | writer.WriteElementString("Y", vec.Y.ToString(Utils.EnUsCulture)); | ||
75 | writer.WriteElementString("Z", vec.Z.ToString(Utils.EnUsCulture)); | ||
76 | writer.WriteEndElement(); | ||
77 | } | ||
78 | |||
79 | private void XWVector4(string name, Vector4 quat) | ||
80 | { | ||
81 | writer.WriteStartElement(name); | ||
82 | writer.WriteElementString("X", quat.X.ToString(Utils.EnUsCulture)); | ||
83 | writer.WriteElementString("Y", quat.Y.ToString(Utils.EnUsCulture)); | ||
84 | writer.WriteElementString("Z", quat.Z.ToString(Utils.EnUsCulture)); | ||
85 | writer.WriteElementString("W", quat.W.ToString(Utils.EnUsCulture)); | ||
86 | writer.WriteEndElement(); | ||
87 | } | ||
88 | |||
89 | public void ToXml2(XmlTextWriter twriter) | ||
90 | { | ||
91 | writer = twriter; | ||
92 | writer.WriteStartElement("PhysicsInertia"); | ||
93 | |||
94 | XWfloat("MASS", TotalMass); | ||
95 | XWVector("CM", CenterOfMass); | ||
96 | XWVector("INERTIA", Inertia); | ||
97 | XWVector4("IROT", InertiaRotation); | ||
98 | |||
99 | writer.WriteEndElement(); | ||
100 | writer = null; | ||
101 | } | ||
102 | |||
103 | XmlReader reader; | ||
104 | |||
105 | private int XRint() | ||
106 | { | ||
107 | return reader.ReadElementContentAsInt(); | ||
108 | } | ||
109 | |||
110 | private float XRfloat() | ||
111 | { | ||
112 | return reader.ReadElementContentAsFloat(); | ||
113 | } | ||
114 | |||
115 | public Vector3 XRvector() | ||
116 | { | ||
117 | Vector3 vec; | ||
118 | reader.ReadStartElement(); | ||
119 | vec.X = reader.ReadElementContentAsFloat(); | ||
120 | vec.Y = reader.ReadElementContentAsFloat(); | ||
121 | vec.Z = reader.ReadElementContentAsFloat(); | ||
122 | reader.ReadEndElement(); | ||
123 | return vec; | ||
124 | } | ||
125 | |||
126 | public Vector4 XRVector4() | ||
127 | { | ||
128 | Vector4 q; | ||
129 | reader.ReadStartElement(); | ||
130 | q.X = reader.ReadElementContentAsFloat(); | ||
131 | q.Y = reader.ReadElementContentAsFloat(); | ||
132 | q.Z = reader.ReadElementContentAsFloat(); | ||
133 | q.W = reader.ReadElementContentAsFloat(); | ||
134 | reader.ReadEndElement(); | ||
135 | return q; | ||
136 | } | ||
137 | |||
138 | public static bool EReadProcessors( | ||
139 | Dictionary<string, Action> processors, | ||
140 | XmlReader xtr) | ||
141 | { | ||
142 | bool errors = false; | ||
143 | |||
144 | string nodeName = string.Empty; | ||
145 | while (xtr.NodeType != XmlNodeType.EndElement) | ||
146 | { | ||
147 | nodeName = xtr.Name; | ||
148 | |||
149 | Action p = null; | ||
150 | if (processors.TryGetValue(xtr.Name, out p)) | ||
151 | { | ||
152 | try | ||
153 | { | ||
154 | p(); | ||
155 | } | ||
156 | catch | ||
157 | { | ||
158 | errors = true; | ||
159 | if (xtr.NodeType == XmlNodeType.EndElement) | ||
160 | xtr.Read(); | ||
161 | } | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | xtr.ReadOuterXml(); // ignore | ||
166 | } | ||
167 | } | ||
168 | |||
169 | return errors; | ||
170 | } | ||
171 | |||
172 | public string ToXml2() | ||
173 | { | ||
174 | using (StringWriter sw = new StringWriter()) | ||
175 | { | ||
176 | using (XmlTextWriter xwriter = new XmlTextWriter(sw)) | ||
177 | { | ||
178 | ToXml2(xwriter); | ||
179 | } | ||
180 | |||
181 | return sw.ToString(); | ||
182 | } | ||
183 | } | ||
184 | |||
185 | public static PhysicsInertiaData FromXml2(string text) | ||
186 | { | ||
187 | if (text == String.Empty) | ||
188 | return null; | ||
189 | |||
190 | UTF8Encoding enc = new UTF8Encoding(); | ||
191 | MemoryStream ms = new MemoryStream(enc.GetBytes(text)); | ||
192 | XmlTextReader xreader = new XmlTextReader(ms); | ||
193 | |||
194 | PhysicsInertiaData v = new PhysicsInertiaData(); | ||
195 | bool error; | ||
196 | |||
197 | v.FromXml2(xreader, out error); | ||
198 | |||
199 | xreader.Close(); | ||
200 | |||
201 | if (error) | ||
202 | return null; | ||
203 | |||
204 | return v; | ||
205 | } | ||
206 | |||
207 | public static PhysicsInertiaData FromXml2(XmlReader reader) | ||
208 | { | ||
209 | PhysicsInertiaData data = new PhysicsInertiaData(); | ||
210 | |||
211 | bool errors = false; | ||
212 | |||
213 | data.FromXml2(reader, out errors); | ||
214 | if (errors) | ||
215 | return null; | ||
216 | |||
217 | return data; | ||
218 | } | ||
219 | |||
220 | private void FromXml2(XmlReader _reader, out bool errors) | ||
221 | { | ||
222 | errors = false; | ||
223 | reader = _reader; | ||
224 | |||
225 | Dictionary<string, Action> m_XmlProcessors = new Dictionary<string, Action>(); | ||
226 | |||
227 | m_XmlProcessors.Add("MASS", ProcessXR_Mass); | ||
228 | m_XmlProcessors.Add("CM", ProcessXR_CM); | ||
229 | m_XmlProcessors.Add("INERTIA", ProcessXR_Inertia); | ||
230 | m_XmlProcessors.Add("IROT", ProcessXR_InertiaRotation); | ||
231 | |||
232 | reader.ReadStartElement("PhysicsInertia", String.Empty); | ||
233 | |||
234 | errors = EReadProcessors( | ||
235 | m_XmlProcessors, | ||
236 | reader); | ||
237 | |||
238 | reader.ReadEndElement(); | ||
239 | reader = null; | ||
240 | } | ||
241 | |||
242 | private void ProcessXR_Mass() | ||
243 | { | ||
244 | TotalMass = XRfloat(); | ||
245 | } | ||
246 | |||
247 | private void ProcessXR_CM() | ||
248 | { | ||
249 | CenterOfMass = XRvector(); | ||
250 | } | ||
251 | |||
252 | private void ProcessXR_Inertia() | ||
253 | { | ||
254 | Inertia = XRvector(); | ||
255 | } | ||
256 | |||
257 | private void ProcessXR_InertiaRotation() | ||
258 | { | ||
259 | InertiaRotation = XRVector4(); | ||
260 | } | ||
261 | } | ||
262 | } | ||