1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
#include "lc_global.h"
#include "lc_traintrack.h"
#include "lc_library.h"
#include "pieceinf.h"
#include "piece.h"
#include "lc_model.h"
#include "lc_application.h"
// todo:
// when moving existing pieces:
// need to check for parts intersecting instead of mouse intersection because it's hard to connect tracks
// look into the snapping that happens when there are no available connections
// fix comments in UpdateFreeMoveTool
// hide some of the 12v tracks to avoid bloat
// auto replace cross when going over a straight section
// set focus connection after adding
std::map<size_t, PieceInfo*> lcTrainTrackInfo::mSleepers;
void lcTrainTrackInfo::Initialize(lcPiecesLibrary* Library)
{
QFile ConfigFile(QLatin1String(":/resources/traintrack.json"));
if (!ConfigFile.open(QIODevice::ReadOnly))
return;
QJsonParseError Error;
QJsonDocument Document = QJsonDocument::fromJson(ConfigFile.readAll(), &Error);
if (Error.error != QJsonParseError::NoError)
{
qDebug() << Error.errorString();
}
QJsonObject Root = Document.object();
if (Root["Version"].toInt() != 1)
return;
QJsonArray JsonPieces = Root["Pieces"].toArray();
for (QJsonArray::const_iterator PiecesIt = JsonPieces.constBegin(); PiecesIt != JsonPieces.constEnd(); ++PiecesIt)
{
QJsonObject JsonPiece = PiecesIt->toObject();
QJsonArray JsonConnections = JsonPiece["Connections"].toArray();
std::vector<lcTrainTrackConnection> Connections;
for (QJsonArray::const_iterator ConnectionIt = JsonConnections.constBegin(); ConnectionIt != JsonConnections.constEnd(); ++ConnectionIt)
{
QJsonObject JsonConnection = ConnectionIt->toObject();
QJsonArray JsonPosition = JsonConnection["Position"].toArray();
lcVector3 Position(JsonPosition[0].toDouble(), JsonPosition[1].toDouble(), JsonPosition[2].toDouble());
float Rotation = JsonConnection["Rotation"].toDouble() * LC_DTOR;
QString ConnectionGroup = JsonConnection["Type"].toString();
lcTrainTrackConnectionSleeper ConnectionSleeper = lcTrainTrackConnectionSleeper::None;
if (ConnectionGroup.startsWith('+'))
{
ConnectionSleeper = lcTrainTrackConnectionSleeper::NeedsSleeper;
ConnectionGroup = ConnectionGroup.mid(1);
}
else if (ConnectionGroup.startsWith('-'))
{
ConnectionSleeper = lcTrainTrackConnectionSleeper::HasSleeper;
ConnectionGroup = ConnectionGroup.mid(1);
}
Connections.emplace_back(lcTrainTrackConnection{ lcMatrix44(lcMatrix33RotationZ(Rotation), Position), { qHash(ConnectionGroup), ConnectionSleeper } });
}
int Color = JsonPiece["Color"].toInt(16);
QJsonArray JsonIDs = JsonPiece["IDs"].toArray();
for (QJsonArray::const_iterator IDIt = JsonIDs.constBegin(); IDIt != JsonIDs.constEnd(); ++IDIt)
{
PieceInfo* Info = Library->FindPiece(IDIt->toString().toLatin1(), nullptr, false, false);
if (Info)
Info->SetTrainTrackInfo(new lcTrainTrackInfo(Connections, Color, true));
}
JsonIDs = JsonPiece["HiddenIDs"].toArray();
for (QJsonArray::const_iterator IDIt = JsonIDs.constBegin(); IDIt != JsonIDs.constEnd(); ++IDIt)
{
PieceInfo* Info = Library->FindPiece(IDIt->toString().toLatin1(), nullptr, false, false);
if (Info)
Info->SetTrainTrackInfo(new lcTrainTrackInfo(Connections, Color, false));
}
}
QJsonObject JsonSleepers = Root["Sleepers"].toObject();
mSleepers.clear();
for (QJsonObject::const_iterator SleepersIt = JsonSleepers.constBegin(); SleepersIt != JsonSleepers.constEnd(); ++SleepersIt)
{
size_t Group = qHash(SleepersIt.key());
QString PartId = SleepersIt->toString();
PieceInfo* SleeperInfo = Library->FindPiece(PartId.toLatin1(), nullptr, false, false);
if (SleeperInfo)
mSleepers[Group] = SleeperInfo;
}
}
int lcTrainTrackInfo::GetPieceConnectionIndex(const lcPiece* Piece1, int ConnectionIndex1, const lcPiece* Piece2)
{
const lcTrainTrackInfo* TrainTrackInfo1 = Piece1->mPieceInfo->GetTrainTrackInfo();
const lcTrainTrackInfo* TrainTrackInfo2 = Piece2->mPieceInfo->GetTrainTrackInfo();
const std::vector<lcTrainTrackConnection>& Connections2 = TrainTrackInfo2->GetConnections();
lcMatrix44 Transform1 = lcMul(TrainTrackInfo1->GetConnections()[ConnectionIndex1].Transform, Piece1->mModelWorld);
for (int ConnectionIndex2 = 0; ConnectionIndex2 < static_cast<int>(Connections2.size()); ConnectionIndex2++)
{
const lcTrainTrackConnection& Connection2 = Connections2[ConnectionIndex2];
const lcMatrix44 Transform2 = lcMul(Connection2.Transform, Piece2->mModelWorld);
if (lcLengthSquared(Transform1.GetTranslation() - Transform2.GetTranslation()) > 0.1f)
continue;
float Dot = lcDot3(Transform1[0], Transform2[0]);
if (Dot < -0.99f && Dot > -1.01f)
return ConnectionIndex2;
}
return -1;
}
std::vector<lcInsertPieceInfo> lcTrainTrackInfo::GetInsertPieceInfo(lcPiece* CurrentPiece, PieceInfo* Info, lcPiece* MovingPiece, int ColorIndex, quint32 PreferredSection, bool AllowNewPieces, std::optional<lcVector3> ClosestPoint)
{
std::vector<lcInsertPieceInfo> Pieces;
const lcTrainTrackInfo* CurrentTrackInfo = CurrentPiece->mPieceInfo->GetTrainTrackInfo();
if (!CurrentTrackInfo || CurrentTrackInfo->GetConnections().empty() || (MovingPiece && Info != MovingPiece->mPieceInfo))
return Pieces;
quint32 ConnectionIndex = 0;
if (ClosestPoint)
{
lcMatrix44 InverseMatrix = lcMatrix44AffineInverse(CurrentPiece->mModelWorld);
lcVector3 LocalClosestPoint = lcMul31(ClosestPoint.value(), InverseMatrix);
float BestDistance = FLT_MAX;
for (quint32 CheckIndex = 0; CheckIndex < CurrentTrackInfo->GetConnections().size(); CheckIndex++)
{
if (CurrentPiece->IsTrainTrackConnected(CheckIndex))
continue;
if (MovingPiece)
{
const std::vector<lcTrainTrackConnection>& MovingConnections = Info->GetTrainTrackInfo()->GetConnections();
bool CanConnect = false;
for (quint32 MovingConnectionIndex = 0; MovingConnectionIndex < MovingConnections.size(); MovingConnectionIndex++)
{
if (!MovingPiece->IsTrainTrackConnected(MovingConnectionIndex) && AreConnectionsCompatible(CurrentTrackInfo->GetConnections()[CheckIndex].Type, MovingConnections[MovingConnectionIndex].Type, AllowNewPieces))
{
CanConnect = true;
break;
}
}
if (!CanConnect)
continue;
}
else
{
if (!Info->GetTrainTrackInfo()->CanConnectTo(CurrentTrackInfo->GetConnections()[CheckIndex].Type, AllowNewPieces))
continue;
}
float Distance = (CurrentTrackInfo->GetConnections()[CheckIndex].Transform.GetTranslation() - LocalClosestPoint).LengthSquared();
if (Distance < BestDistance)
{
ConnectionIndex = CheckIndex;
BestDistance = Distance;
}
}
if (BestDistance == FLT_MAX)
return Pieces;
}
else
{
if (PreferredSection != LC_PIECE_SECTION_INVALID && PreferredSection >= LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST)
ConnectionIndex = PreferredSection - LC_PIECE_SECTION_TRAIN_TRACK_CONNECTION_FIRST;
quint32 CheckIndex;
for (CheckIndex = 0; CheckIndex < CurrentTrackInfo->GetConnections().size(); CheckIndex++)
if (!CurrentPiece->IsTrainTrackConnected((ConnectionIndex + CheckIndex) % CurrentTrackInfo->GetConnections().size()))
break;
if (CheckIndex == CurrentTrackInfo->GetConnections().size())
return Pieces;
ConnectionIndex = (ConnectionIndex + CheckIndex) % CurrentTrackInfo->GetConnections().size();
}
const lcTrainTrackConnectionType& CurrentConnectionType = CurrentTrackInfo->GetConnections()[ConnectionIndex].Type;
const std::vector<lcTrainTrackConnection>& NewConnections = Info->GetTrainTrackInfo()->GetConnections();
quint32 NewConnectionIndex;// = ConnectionIndex ? 0 : 1;
for (NewConnectionIndex = 0; NewConnectionIndex < NewConnections.size(); NewConnectionIndex++)
{
if (MovingPiece)
{
if (MovingPiece->IsTrainTrackConnected(NewConnectionIndex))
continue;
const std::vector<lcTrainTrackConnection>& MovingConnections = Info->GetTrainTrackInfo()->GetConnections();
if (AreConnectionsCompatible(CurrentTrackInfo->GetConnections()[ConnectionIndex].Type, MovingConnections[NewConnectionIndex].Type, AllowNewPieces))
break;
}
else
{
if (AreConnectionsCompatible(CurrentConnectionType, NewConnections[NewConnectionIndex].Type, AllowNewPieces))
break;
}
}
if (NewConnectionIndex == NewConnections.size())
return Pieces;
if (CurrentConnectionType.Sleeper == lcTrainTrackConnectionSleeper::NeedsSleeper && NewConnections[NewConnectionIndex].Type.Sleeper == lcTrainTrackConnectionSleeper::NeedsSleeper)
{
std::map<size_t, PieceInfo*>::const_iterator SleeperIt = mSleepers.find(CurrentConnectionType.Group);
if (SleeperIt == mSleepers.end())
return Pieces;
PieceInfo* SleeperInfo = SleeperIt->second;
if (!SleeperInfo)
return Pieces;
std::optional<lcMatrix44> SleeperTransform = GetConnectionTransform(CurrentPiece->mPieceInfo, CurrentPiece->mModelWorld, ConnectionIndex, SleeperInfo, 0);
if (!SleeperTransform)
return Pieces;
std::optional<lcMatrix44> Transform = GetConnectionTransform(SleeperInfo, SleeperTransform.value(), 1, Info, NewConnectionIndex);
if (Transform)
{
Pieces.emplace_back(lcInsertPieceInfo{ SleeperInfo, SleeperTransform.value(), lcGetColorIndex(SleeperInfo->GetTrainTrackInfo()->GetColorCode()) });
Pieces.emplace_back(lcInsertPieceInfo{ Info, Transform.value(), ColorIndex });
}
}
else
{
std::optional<lcMatrix44> Transform = GetConnectionTransform(CurrentPiece->mPieceInfo, CurrentPiece->mModelWorld, ConnectionIndex, Info, NewConnectionIndex);
if (Transform)
Pieces.emplace_back(lcInsertPieceInfo{ Info, Transform.value(), ColorIndex });
}
return Pieces;
}
std::optional<lcMatrix44> lcTrainTrackInfo::GetConnectionTransform(PieceInfo* CurrentInfo, const lcMatrix44& CurrentTransform, quint32 CurrentConnectionIndex, PieceInfo* Info, quint32 NewConnectionIndex)
{
if (!CurrentInfo || !Info)
return std::nullopt;
const lcTrainTrackInfo* CurrentTrackInfo = CurrentInfo->GetTrainTrackInfo();
if (!CurrentTrackInfo || CurrentTrackInfo->GetConnections().empty())
return std::nullopt;
if (CurrentConnectionIndex >= CurrentTrackInfo->GetConnections().size())
return std::nullopt;
lcTrainTrackInfo* NewTrackInfo = Info->GetTrainTrackInfo();
if (!NewTrackInfo || NewConnectionIndex >= NewTrackInfo->mConnections.size())
return std::nullopt;
lcMatrix44 Transform;
// if (TrainTrackType != lcTrainTrackType::Left)
// Transform = NewTrackInfo->mConnections[NewConnectionIndex].Transform;
// else
// {
Transform = lcMatrix44AffineInverse(NewTrackInfo->mConnections[NewConnectionIndex].Transform);
Transform = lcMul(Transform, lcMatrix44RotationZ(LC_PI));
// }
Transform = lcMul(Transform, CurrentTrackInfo->GetConnections()[CurrentConnectionIndex].Transform);
Transform = lcMul(Transform, CurrentTransform);
return Transform;
}
std::optional<lcMatrix44> lcTrainTrackInfo::CalculateTransformToConnection(const lcMatrix44& ConnectionTransform, PieceInfo* Info, quint32 ConnectionIndex)
{
lcTrainTrackInfo* TrackInfo = Info->GetTrainTrackInfo();
if (!TrackInfo || ConnectionIndex >= TrackInfo->mConnections.size())
return std::nullopt;
lcMatrix44 Transform;
// if (TrainTrackType != lcTrainTrackType::Left)
// Transform = NewTrackInfo->mConnections[NewConnectionIndex].Transform;
// else
// {
Transform = lcMatrix44AffineInverse(TrackInfo->mConnections[ConnectionIndex].Transform);
// Transform = lcMul(Transform, lcMatrix44RotationZ(LC_PI));
// }
Transform = lcMul(Transform, ConnectionTransform);
// Transform = lcMul(Transform, CurrentTrackInfo->GetConnections()[CurrentConnectionIndex].Transform);
// Transform = lcMul(Transform, CurrentPiece->mModelWorld);
return Transform;
}
|