ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

path.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/resources/path.h"
00024 
00025 #include "engines/stark/formats/xrc.h"
00026 
00027 #include "engines/stark/resources/floor.h"
00028 
00029 #include "engines/stark/services/global.h"
00030 #include "engines/stark/services/services.h"
00031 
00032 namespace Stark {
00033 namespace Resources {
00034 
00035 Object *Path::construct(Object *parent, byte subType, uint16 index, const Common::String &name) {
00036     switch (subType) {
00037     case kPath2D:
00038         return new Path2D(parent, subType, index, name);
00039     case kPath3D:
00040         return new Path3D(parent, subType, index, name);
00041     default:
00042         error("Unknown path subtype %d", subType);
00043     }
00044 }
00045 
00046 Path::~Path() {
00047 }
00048 
00049 Path::Path(Object *parent, byte subType, uint16 index, const Common::String &name) :
00050         Object(parent, subType, index, name),
00051         _field_30(0) {
00052     _type = TYPE;
00053 }
00054 
00055 void Path::readData(Formats::XRCReadStream *stream) {
00056     _field_30 = stream->readUint32LE();
00057 }
00058 
00059 void Path::printData() {
00060     debug("field_30: %d", _field_30);
00061 }
00062 
00063 float Path::getEdgeLength(uint edgeIndex) const {
00064     Math::Vector3d edgeStart = getVertexPosition(edgeIndex);
00065     Math::Vector3d edgeEnd = getVertexPosition(edgeIndex + 1);
00066 
00067     return edgeStart.getDistanceTo(edgeEnd);
00068 }
00069 
00070 float Path::getWeightedEdgeLength(uint edgeIndex) const {
00071     float length = getEdgeLength(edgeIndex);
00072     float startWeight = getVertexWeight(edgeIndex);
00073     float endWeight = getVertexWeight(edgeIndex + 1);
00074 
00075     return 2000.0 * length / (startWeight + endWeight);
00076 }
00077 
00078 Math::Vector3d Path::getWeightedPositionInEdge(uint edgeIndex, float positionInEdge) {
00079     float edgeLength = getEdgeLength(edgeIndex);
00080     float weightedEdgeLength = getWeightedEdgeLength(edgeIndex);
00081 
00082     float startWeight = getVertexWeight(edgeIndex);
00083     float endWeight = getVertexWeight(edgeIndex + 1);
00084 
00085     float weightedEdgePosition = ((endWeight - startWeight) / (2 * weightedEdgeLength) * positionInEdge + startWeight) * 0.001
00086           * positionInEdge / edgeLength;
00087 
00088     Math::Vector3d edgeStart = getVertexPosition(edgeIndex);
00089     Math::Vector3d edgeEnd = getVertexPosition(edgeIndex + 1);
00090 
00091     return edgeEnd * weightedEdgePosition + edgeStart * (1.0 - weightedEdgePosition);
00092 }
00093 
00094 float Path::getSortKey() const {
00095     return 0;
00096 }
00097 
00098 Math::Vector3d Path::getEdgeDirection(uint edgeIndex) const {
00099     return Math::Vector3d();
00100 }
00101 
00102 
00103 Path2D::Path2D(Object *parent, byte subType, uint16 index, const Common::String &name) :
00104         Path(parent, subType, index, name) {
00105 }
00106 
00107 void Path2D::readData(Formats::XRCReadStream *stream) {
00108     Path::readData(stream);
00109 
00110     uint32 vertexCount = stream->readUint32LE();
00111     for (uint i = 0; i < vertexCount; i++) {
00112         Vertex vertex;
00113         vertex.weight = stream->readFloatLE();
00114         vertex.position = stream->readPoint();
00115 
00116         _vertices.push_back(vertex);
00117     }
00118 
00119     stream->readUint32LE(); // Unused in the original
00120 }
00121 
00122 void Path2D::printData() {
00123     Path::printData();
00124 
00125     for (uint i = 0; i < _vertices.size(); i++) {
00126         debug("vertex[%d]: (x %d, y %d), weight: %f", i,
00127               _vertices[i].position.x, _vertices[i].position.y, _vertices[i].weight);
00128     }
00129 }
00130 
00131 Path2D::~Path2D() {
00132 }
00133 
00134 uint Path2D::getEdgeCount() const {
00135     return _vertices.size() - 1;
00136 }
00137 
00138 Math::Vector3d Path2D::getVertexPosition(uint vertexIndex) const {
00139     Common::Point point = _vertices[vertexIndex].position;
00140     return Math::Vector3d(point.x, point.y, 0.0);
00141 }
00142 
00143 float Path2D::getVertexWeight(uint vertexIndex) const {
00144     return _vertices[vertexIndex].weight;
00145 }
00146 
00147 Path3D::Path3D(Object *parent, byte subType, uint16 index, const Common::String &name) :
00148         Path(parent, subType, index, name),
00149         _sortKey(0) {
00150 }
00151 
00152 void Path3D::readData(Formats::XRCReadStream *stream) {
00153     Path::readData(stream);
00154 
00155     uint32 vertexCount = stream->readUint32LE();
00156     for (uint i = 0; i < vertexCount; i++) {
00157         Vertex vertex;
00158         vertex.weight = stream->readFloatLE();
00159         vertex.position = stream->readVector3();
00160 
00161         _vertices.push_back(vertex);
00162     }
00163 
00164     _sortKey = stream->readFloatLE();
00165 }
00166 
00167 void Path3D::printData() {
00168     Path::printData();
00169 
00170     for (uint i = 0; i < _vertices.size(); i++) {
00171         debug("vertex[%d]: (x %f, y %f, z %f), weight: %f", i,
00172               _vertices[i].position.x(), _vertices[i].position.y(), _vertices[i].position.z(), _vertices[i].weight);
00173     }
00174 
00175     debug("sortKey: %f", _sortKey);
00176 }
00177 
00178 Path3D::~Path3D() {
00179 }
00180 
00181 uint Path3D::getEdgeCount() const {
00182     return _vertices.size() - 1;
00183 }
00184 
00185 Math::Vector3d Path3D::getVertexPosition(uint vertexIndex) const {
00186     return _vertices[vertexIndex].position;
00187 }
00188 
00189 float Path3D::getVertexWeight(uint vertexIndex) const {
00190     return _vertices[vertexIndex].weight;
00191 }
00192 
00193 float Path3D::getSortKey() const {
00194     return _sortKey;
00195 }
00196 
00197 Math::Vector3d Path3D::getEdgeDirection(uint edgeIndex) const {
00198     Math::Vector3d direction = getVertexPosition(edgeIndex) - getVertexPosition(edgeIndex + 1);
00199     direction.normalize();
00200     return direction;
00201 }
00202 
00203 Math::Vector3d Path3D::getVertexPosition3D(uint vertexIndex, int32 *faceIndex) {
00204     Math::Vector3d vertex = getVertexPosition(vertexIndex);
00205 
00206     Floor *floor = StarkGlobal->getCurrent()->getFloor();
00207     if (floor) {
00208         int32 face = floor->findFaceContainingPoint(vertex);
00209         if (face >= 0) {
00210             floor->computePointHeightInFace(vertex, face);
00211         }
00212 
00213         if (faceIndex) {
00214             *faceIndex = face;
00215         }
00216     }
00217 
00218     return vertex;
00219 }
00220 
00221 } // End of namespace Resources
00222 } // End of namespace Stark


Generated on Sat Mar 16 2019 05:01:49 for ResidualVM by doxygen 1.7.1
curved edge   curved edge