Skip to content

Commit c266527

Browse files
committed
Clean code
1 parent 9891cc2 commit c266527

File tree

9 files changed

+279
-285
lines changed

9 files changed

+279
-285
lines changed

R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

4-
viewer <- function(df, hnof) {
5-
invisible(.Call(`_lidRviewer_viewer`, df, hnof))
4+
viewer <- function(df, detach, hnof) {
5+
invisible(.Call(`_lidRviewer_viewer`, df, detach, hnof))
66
}
77

R/plot.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#'
1515
#' @param x a point cloud with minimally 3 columns named X,Y,Z
1616
#' @param y Unused (inherited from R base)
17-
#' @param ... unused
17+
#' @param ... Support detach = TRUE
1818
#' @export
1919
#' @method plot LAS
2020
#' @importClassesFrom lidR LAS
@@ -23,7 +23,9 @@
2323
#' @md
2424
setMethod("plot", signature(x = "LAS", y = "missing"), function(x, y, ...)
2525
{
26-
viewer(x@data, "")
26+
p = list(...)
27+
detach = isTRUE(p$detach)
28+
viewer(x@data, detach, "")
2729
})
2830

2931
render = function(f)
@@ -32,8 +34,7 @@ render = function(f)
3234
las = lidR::readLAS(x)
3335
hnof = paste0(substr(x, 1, nchar(x) - 3), "hno")
3436
f = if (file.exists(hnof)) hnof else x
35-
print(f)
36-
viewer(las@data, f)
37+
viewer(las@data, FALSE, f)
3738
}
3839

3940

src/RcppExports.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
1111
#endif
1212

1313
// viewer
14-
void viewer(DataFrame df, std::string hnof);
15-
RcppExport SEXP _lidRviewer_viewer(SEXP dfSEXP, SEXP hnofSEXP) {
14+
void viewer(DataFrame df, bool detach, std::string hnof);
15+
RcppExport SEXP _lidRviewer_viewer(SEXP dfSEXP, SEXP detachSEXP, SEXP hnofSEXP) {
1616
BEGIN_RCPP
1717
Rcpp::RNGScope rcpp_rngScope_gen;
1818
Rcpp::traits::input_parameter< DataFrame >::type df(dfSEXP);
19+
Rcpp::traits::input_parameter< bool >::type detach(detachSEXP);
1920
Rcpp::traits::input_parameter< std::string >::type hnof(hnofSEXP);
20-
viewer(df, hnof);
21+
viewer(df, detach, hnof);
2122
return R_NilValue;
2223
END_RCPP
2324
}
2425

2526
static const R_CallMethodDef CallEntries[] = {
26-
{"_lidRviewer_viewer", (DL_FUNC) &_lidRviewer_viewer, 2},
27+
{"_lidRviewer_viewer", (DL_FUNC) &_lidRviewer_viewer, 3},
2728
{NULL, NULL, 0}
2829
};
2930

src/camera.cpp

Lines changed: 26 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "camera.h"
2-
#include "sdlglutils.h"
3-
#include "Frustum.h"
42

5-
#include <Rcpp.h>
63
#include <cmath>
74

85
#include <GL/glu.h>
@@ -139,97 +136,6 @@ static bool InvertMatrix(const GLfloat m[16], GLfloat invOut[16])
139136

140137
Camera::Camera()
141138
{
142-
const char *hand1[] =
143-
{
144-
/* width height num_colors chars_per_pixel */
145-
" 16 16 3 1 ",
146-
/* colors */
147-
"X c #000000",
148-
". c #ffffff",
149-
" c None",
150-
/* pixels */
151-
" XX ",
152-
" XX X..XXX ",
153-
" X..XX..X..X ",
154-
" X..XX..X..X X ",
155-
" X..X..X..XX.X",
156-
" X..X..X..X..X",
157-
" XX X.......X..X",
158-
"X..XX..........X",
159-
"X...X.........X ",
160-
" X............X ",
161-
" X...........X ",
162-
" X..........X ",
163-
" X.........X ",
164-
" X.......X ",
165-
" X......X ",
166-
" X......X ",
167-
"0,0"
168-
};
169-
170-
const char *hand2[] =
171-
{
172-
/* width height num_colors chars_per_pixel */
173-
" 16 16 3 1 ",
174-
/* colors */
175-
"X c #000000",
176-
". c #ffffff",
177-
" c None",
178-
/* pixels */
179-
" ",
180-
" ",
181-
" ",
182-
" ",
183-
" XX XX XX ",
184-
" X..X..X..XX ",
185-
" X........X.X ",
186-
" X.........X ",
187-
" XX.........X ",
188-
" X...........X ",
189-
" X...........X ",
190-
" X..........X ",
191-
" X.........X ",
192-
" X.......X ",
193-
" X......X ",
194-
" X......X ",
195-
"0,0"
196-
};
197-
198-
const char *move[] =
199-
{
200-
/* width height num_colors chars_per_pixel */
201-
" 16 16 1 ",
202-
/* colors */
203-
"X c #000000",
204-
". c #ffffff",
205-
" c None",
206-
/* pixels */
207-
" XX ",
208-
" X..X ",
209-
" X....X ",
210-
" X......X ",
211-
" X XX..XX X ",
212-
" X.X X..X X.X ",
213-
" X..XXX..XXX..X ",
214-
"X..............X",
215-
"X..............X",
216-
" X..XXX..XXX..X ",
217-
" X.X X..X X.X ",
218-
" X XX..XX X ",
219-
" X......X ",
220-
" X....X ",
221-
" X..X ",
222-
" XX ",
223-
"0,0"
224-
};
225-
226-
_hand1 = cursorFromXPM(hand1);
227-
_hand2 = cursorFromXPM(hand2);
228-
_move = cursorFromXPM(move);
229-
230-
SDL_SetCursor(_hand1);
231-
holdleft = false;
232-
holdright = false;
233139
angleY = 20;
234140
angleZ = -30;
235141
deltaX = 0;
@@ -242,94 +148,43 @@ Camera::Camera()
242148
zoomSensivity = 30;
243149
}
244150

245-
void Camera::OnMouseMotion(const SDL_MouseMotionEvent & event)
151+
void Camera::rotate(int xrel, int yrel)
246152
{
247-
if (holdleft) // Rotate
248-
{
249-
//printf("Rotate\n");
250-
angleZ += event.xrel*rotateSensivity;
251-
angleY += event.yrel*rotateSensivity;
153+
angleZ += xrel*rotateSensivity;
154+
angleY += yrel*rotateSensivity;
252155

253-
if (angleY > 90)
254-
angleY = 90;
255-
else if (angleY < -90)
256-
angleY = -90;
156+
if (angleY > 90)
157+
angleY = 90;
158+
else if (angleY < -90)
159+
angleY = -90;
257160

258-
changed = true;
259-
}
260-
else if (holdright) // Pan
261-
{
262-
panSensivity = distance*0.01;
263-
//printf("Pan\n");
264-
deltaX += event.xrel*panSensivity;
265-
deltaY -= event.yrel*panSensivity;
266-
changed = true;
267-
}
161+
changed = true;
268162
}
269163

270-
void Camera::OnMouseEvent(const SDL_MouseButtonEvent &event, const SDL_MouseWheelEvent &event_wheel)
164+
void Camera::pan(int xrel, int yrel)
271165
{
272-
//printf("Mouse event\n");
166+
panSensivity = distance*0.01;
167+
deltaX += xrel*panSensivity;
168+
deltaY -= yrel*panSensivity;
169+
changed = true;
170+
}
273171

274-
if (event.button == SDL_BUTTON_LEFT)
275-
{
276-
if ((holdleft) && (event.type == SDL_MOUSEBUTTONUP))
277-
{
278-
holdleft = false;
279-
SDL_SetCursor(_hand1);
280-
}
281-
else if ((!holdleft) && (event.type == SDL_MOUSEBUTTONDOWN))
282-
{
283-
holdleft = true;
284-
SDL_SetCursor(_hand2);
285-
}
286-
}
287-
else if (event.button == SDL_BUTTON_RIGHT)
172+
void Camera::zoom(int zrel)
173+
{
174+
if (zrel > 0)
288175
{
289-
if ((holdright) && (event.type == SDL_MOUSEBUTTONUP))
290-
{
291-
holdright = false;
292-
SDL_SetCursor(_hand1);
293-
}
294-
else if ((!holdright) && (event.type == SDL_MOUSEBUTTONDOWN))
295-
{
296-
holdright = true;
297-
SDL_SetCursor(_move);
298-
}
176+
distance += zoomSensivity;
177+
panSensivity = distance * 0.001;
178+
zoomSensivity = distance * 0.05;
299179
}
300-
301-
// Handle mouse wheel event separately
302-
if (event_wheel.type == SDL_MOUSEWHEEL)
180+
else if (zrel < 0)
303181
{
304-
//printf("Wheel even\n");
305-
306-
if (event_wheel.y > 0)
307-
{
308-
//printf("Wheel up\n");
309-
distance += zoomSensivity;
310-
panSensivity = distance * 0.001;
311-
zoomSensivity = distance * 0.05;
312-
changed = true;
313-
}
314-
else if (event_wheel.y < 0)
315-
{
316-
//printf("Wheel down\n");
317-
distance -= zoomSensivity;
318-
panSensivity = distance * 0.001;
319-
zoomSensivity = distance * 0.05;
320-
changed = true;
321-
}
182+
distance -= zoomSensivity;
183+
panSensivity = distance * 0.001;
184+
zoomSensivity = distance * 0.05;
322185
}
323-
}
324186

325-
void Camera::OnKeyboard(const SDL_KeyboardEvent & event)
326-
{
327-
if ((event.type == SDL_KEYDOWN)&&(event.keysym.sym == SDLK_HOME))
328-
{
329-
angleY = 0;
330-
angleZ = 0;
331-
changed = true;
332-
}
187+
changed = true;
333188
}
334189

335190
void Camera::setPanSensivity(double sensivity)
@@ -360,13 +215,6 @@ void Camera::setDistance(double dist)
360215
distance = dist;
361216
}
362217

363-
Camera::~Camera()
364-
{
365-
SDL_FreeCursor(_hand1);
366-
SDL_FreeCursor(_hand2);
367-
SDL_SetCursor(NULL);
368-
}
369-
370218
void Camera::look()
371219
{
372220
glTranslated(deltaX, deltaY, 0.0);
@@ -377,6 +225,7 @@ void Camera::look()
377225

378226
frustum.CalculateFrustum();
379227

228+
// Get the camera position
380229
GLfloat viewMatrix[16];
381230
glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);
382231

src/camera.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
#define CAMERA_H
33

44
#include "Frustum.h"
5-
#include <SDL2/SDL.h>
65

76
class Camera
87
{
98
public:
109
Camera();
11-
~Camera();
12-
13-
void OnMouseMotion(const SDL_MouseMotionEvent & event);
14-
void OnMouseEvent(const SDL_MouseButtonEvent & event, const SDL_MouseWheelEvent & event_wheel);
15-
void OnKeyboard(const SDL_KeyboardEvent & event);
1610

11+
void rotate(int xrel, int yrel);
12+
void pan(int xrel, int yrel);
13+
void zoom(int zrel);
1714
void look();
1815
void setRotateSensivity(double sensivity);
1916
void setPanSensivity(double sensivity);
@@ -36,11 +33,6 @@ class Camera
3633
double deltaX;
3734
double deltaY;
3835
double deltaZ;
39-
bool holdleft;
40-
bool holdright;
41-
SDL_Cursor * _hand1;
42-
SDL_Cursor * _hand2;
43-
SDL_Cursor * _move;
4436

4537
private:
4638
CFrustum frustum;

0 commit comments

Comments
 (0)