File: PointFile.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (176 lines) | stat: -rw-r--r-- 3,465 bytes parent folder | download | duplicates (2)
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
#include "PointFile.h"

#include "i18n.h"
#include "iscenegraph.h"
#include "itextstream.h"
#include "icameraview.h"
#include "imap.h"
#include "iorthoview.h"
#include <fstream>
#include <iostream>

#include "math/Matrix4.h"
#include "math/Vector3.h"
#include "map/Map.h"
#include "scene/PointTrace.h"
#include <fmt/format.h>

#include "module/StaticModule.h"
#include "command/ExecutionFailure.h"

namespace map
{

namespace
{
    const Colour4b RED(255, 0, 0, 1);
}

// Constructor
PointFile::PointFile() :
	_curPos(0),
    _renderable(_points)
{
    GlobalCommandSystem().addCommand(
        "NextLeakSpot", sigc::mem_fun(*this, &PointFile::nextLeakSpot)
    );
    GlobalCommandSystem().addCommand(
        "PrevLeakSpot", sigc::mem_fun(*this, &PointFile::prevLeakSpot)
    );
}

PointFile::~PointFile()
{
}

void PointFile::onMapEvent(IMap::MapEvent ev)
{
	if (ev == IMap::MapUnloading || ev == IMap::MapSaved)
	{
        show({});
	}
}

bool PointFile::isVisible() const
{
	return !_points.empty();
}

void PointFile::show(const fs::path& pointfile)
{
	// Update the status if required
	if (!pointfile.empty())
	{
		// Parse the pointfile from disk
		parse(pointfile);

        // Construct shader if needed, and activate rendering
        auto renderSystem = GlobalMapModule().getRoot()->getRenderSystem();

        if (renderSystem)
        {
            _renderable.update(renderSystem->capture(BuiltInShaderType::PointTraceLines));
        }
	}
	else if (isVisible())
    {
        _points.clear();
        _renderable.clear();
	}

	// Regardless whether hide or show, we reset the current position
	_curPos = 0;

	// Redraw the scene
	SceneChangeNotify();
}

void PointFile::parse(const fs::path& pointfile)
{
    // Open the first pointfile and get its input stream if possible
	std::ifstream inFile(pointfile);
	if (!inFile)
	{
        throw cmd::ExecutionFailure(
            fmt::format(_("Could not open pointfile: {0}"), pointfile.string())
        );
    }

    // Construct vertices from parsed point data
    PointTrace trace(inFile);
    for (auto pos: trace.points())
    {
        _points.emplace_back(pos, RED);
    }
}

// advance camera to previous point
void PointFile::advance(bool forward)
{
	if (!isVisible())
	{
		return;
	}

	if (forward)
	{
		if (_curPos + 2 >= _points.size())
		{
			rMessage() << "End of pointfile" << std::endl;
			return;
		}

		_curPos++;
	}
	else // Backward movement
	{
		if (_curPos == 0)
		{
			rMessage() << "Start of pointfile" << std::endl;
			return;
		}

		_curPos--;
	}

	try
	{
		auto& cam = GlobalCameraManager().getActiveView();

		cam.setCameraOrigin(_points[_curPos].vertex);

		if (module::GlobalModuleRegistry().moduleExists(MODULE_ORTHOVIEWMANAGER))
		{
			GlobalOrthoViewManager().setOrigin(_points[_curPos].vertex);
		}

		{
			Vector3 dir((_points[_curPos + 1].vertex - cam.getCameraOrigin()).getNormalised());
			Vector3 angles(cam.getCameraAngles());

			angles[camera::CAMERA_YAW] = radians_to_degrees(atan2(dir[1], dir[0]));
			angles[camera::CAMERA_PITCH] = radians_to_degrees(asin(dir[2]));

			cam.setCameraAngles(angles);
		}

		// Redraw the scene
		SceneChangeNotify();
	}
	catch (const std::runtime_error& ex)
	{
		rError() << "Cannot set camera view position: " << ex.what() << std::endl;
	}
}

void PointFile::nextLeakSpot(const cmd::ArgumentList& args)
{
	advance(true);
}

void PointFile::prevLeakSpot(const cmd::ArgumentList& args)
{
	advance(false);
}

} // namespace map