aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/PhysicsInertia.cs
diff options
context:
space:
mode:
authoronefang2019-05-19 21:24:15 +1000
committeronefang2019-05-19 21:24:15 +1000
commit5e4d6cab00cb29cd088ab7b62ab13aff103b64cb (patch)
treea9fbc62df9eb2d1d9ba2698d8552eae71eca20d8 /OpenSim/Framework/PhysicsInertia.cs
parentAdd a build script. (diff)
downloadopensim-SC_OLD-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.zip
opensim-SC_OLD-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.gz
opensim-SC_OLD-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.bz2
opensim-SC_OLD-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.xz
Dump OpenSim 0.9.0.1 into it's own branch.
Diffstat (limited to 'OpenSim/Framework/PhysicsInertia.cs')
-rw-r--r--OpenSim/Framework/PhysicsInertia.cs263
1 files changed, 263 insertions, 0 deletions
diff --git a/OpenSim/Framework/PhysicsInertia.cs b/OpenSim/Framework/PhysicsInertia.cs
new file mode 100644
index 0000000..fa83de8
--- /dev/null
+++ b/OpenSim/Framework/PhysicsInertia.cs
@@ -0,0 +1,263 @@
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
28using System;
29using System.Collections.Generic;
30using OpenMetaverse;
31using System.Text;
32using System.IO;
33using System.Xml;
34
35namespace 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(Culture.FormatProvider));
68 }
69
70 private void XWVector(string name, Vector3 vec)
71 {
72 writer.WriteStartElement(name);
73 writer.WriteElementString("X", vec.X.ToString(Culture.FormatProvider));
74 writer.WriteElementString("Y", vec.Y.ToString(Culture.FormatProvider));
75 writer.WriteElementString("Z", vec.Z.ToString(Culture.FormatProvider));
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(Culture.FormatProvider));
83 writer.WriteElementString("Y", quat.Y.ToString(Culture.FormatProvider));
84 writer.WriteElementString("Z", quat.Z.ToString(Culture.FormatProvider));
85 writer.WriteElementString("W", quat.W.ToString(Culture.FormatProvider));
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 bool error;
191 PhysicsInertiaData v;
192 UTF8Encoding enc = new UTF8Encoding();
193 using(MemoryStream ms = new MemoryStream(enc.GetBytes(text)))
194 using(XmlTextReader xreader = new XmlTextReader(ms))
195 {
196 xreader.ProhibitDtd = true;
197
198 v = new PhysicsInertiaData();
199 v.FromXml2(xreader, out error);
200 }
201
202 if (error)
203 return null;
204
205 return v;
206 }
207
208 public static PhysicsInertiaData FromXml2(XmlReader reader)
209 {
210 PhysicsInertiaData data = new PhysicsInertiaData();
211
212 bool errors = false;
213
214 data.FromXml2(reader, out errors);
215 if (errors)
216 return null;
217
218 return data;
219 }
220
221 private void FromXml2(XmlReader _reader, out bool errors)
222 {
223 errors = false;
224 reader = _reader;
225
226 Dictionary<string, Action> m_XmlProcessors = new Dictionary<string, Action>();
227
228 m_XmlProcessors.Add("MASS", ProcessXR_Mass);
229 m_XmlProcessors.Add("CM", ProcessXR_CM);
230 m_XmlProcessors.Add("INERTIA", ProcessXR_Inertia);
231 m_XmlProcessors.Add("IROT", ProcessXR_InertiaRotation);
232
233 reader.ReadStartElement("PhysicsInertia", String.Empty);
234
235 errors = EReadProcessors(
236 m_XmlProcessors,
237 reader);
238
239 reader.ReadEndElement();
240 reader = null;
241 }
242
243 private void ProcessXR_Mass()
244 {
245 TotalMass = XRfloat();
246 }
247
248 private void ProcessXR_CM()
249 {
250 CenterOfMass = XRvector();
251 }
252
253 private void ProcessXR_Inertia()
254 {
255 Inertia = XRvector();
256 }
257
258 private void ProcessXR_InertiaRotation()
259 {
260 InertiaRotation = XRVector4();
261 }
262 }
263}