Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Romain committed Aug 28, 2024
1 parent 9891cc2 commit c266527
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 285 deletions.
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

viewer <- function(df, hnof) {
invisible(.Call(`_lidRviewer_viewer`, df, hnof))
viewer <- function(df, detach, hnof) {
invisible(.Call(`_lidRviewer_viewer`, df, detach, hnof))
}

9 changes: 5 additions & 4 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#'
#' @param x a point cloud with minimally 3 columns named X,Y,Z
#' @param y Unused (inherited from R base)
#' @param ... unused
#' @param ... Support detach = TRUE
#' @export
#' @method plot LAS
#' @importClassesFrom lidR LAS
Expand All @@ -23,7 +23,9 @@
#' @md
setMethod("plot", signature(x = "LAS", y = "missing"), function(x, y, ...)
{
viewer(x@data, "")
p = list(...)
detach = isTRUE(p$detach)
viewer(x@data, detach, "")
})

render = function(f)
Expand All @@ -32,8 +34,7 @@ render = function(f)
las = lidR::readLAS(x)
hnof = paste0(substr(x, 1, nchar(x) - 3), "hno")
f = if (file.exists(hnof)) hnof else x
print(f)
viewer(las@data, f)
viewer(las@data, FALSE, f)
}


Expand Down
9 changes: 5 additions & 4 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// viewer
void viewer(DataFrame df, std::string hnof);
RcppExport SEXP _lidRviewer_viewer(SEXP dfSEXP, SEXP hnofSEXP) {
void viewer(DataFrame df, bool detach, std::string hnof);
RcppExport SEXP _lidRviewer_viewer(SEXP dfSEXP, SEXP detachSEXP, SEXP hnofSEXP) {
BEGIN_RCPP
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< DataFrame >::type df(dfSEXP);
Rcpp::traits::input_parameter< bool >::type detach(detachSEXP);
Rcpp::traits::input_parameter< std::string >::type hnof(hnofSEXP);
viewer(df, hnof);
viewer(df, detach, hnof);
return R_NilValue;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_lidRviewer_viewer", (DL_FUNC) &_lidRviewer_viewer, 2},
{"_lidRviewer_viewer", (DL_FUNC) &_lidRviewer_viewer, 3},
{NULL, NULL, 0}
};

Expand Down
203 changes: 26 additions & 177 deletions src/camera.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "camera.h"
#include "sdlglutils.h"
#include "Frustum.h"

#include <Rcpp.h>
#include <cmath>

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

Camera::Camera()
{
const char *hand1[] =
{
/* width height num_colors chars_per_pixel */
" 16 16 3 1 ",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
" XX ",
" XX X..XXX ",
" X..XX..X..X ",
" X..XX..X..X X ",
" X..X..X..XX.X",
" X..X..X..X..X",
" XX X.......X..X",
"X..XX..........X",
"X...X.........X ",
" X............X ",
" X...........X ",
" X..........X ",
" X.........X ",
" X.......X ",
" X......X ",
" X......X ",
"0,0"
};

const char *hand2[] =
{
/* width height num_colors chars_per_pixel */
" 16 16 3 1 ",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
" ",
" ",
" ",
" ",
" XX XX XX ",
" X..X..X..XX ",
" X........X.X ",
" X.........X ",
" XX.........X ",
" X...........X ",
" X...........X ",
" X..........X ",
" X.........X ",
" X.......X ",
" X......X ",
" X......X ",
"0,0"
};

const char *move[] =
{
/* width height num_colors chars_per_pixel */
" 16 16 1 ",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
" XX ",
" X..X ",
" X....X ",
" X......X ",
" X XX..XX X ",
" X.X X..X X.X ",
" X..XXX..XXX..X ",
"X..............X",
"X..............X",
" X..XXX..XXX..X ",
" X.X X..X X.X ",
" X XX..XX X ",
" X......X ",
" X....X ",
" X..X ",
" XX ",
"0,0"
};

_hand1 = cursorFromXPM(hand1);
_hand2 = cursorFromXPM(hand2);
_move = cursorFromXPM(move);

SDL_SetCursor(_hand1);
holdleft = false;
holdright = false;
angleY = 20;
angleZ = -30;
deltaX = 0;
Expand All @@ -242,94 +148,43 @@ Camera::Camera()
zoomSensivity = 30;
}

void Camera::OnMouseMotion(const SDL_MouseMotionEvent & event)
void Camera::rotate(int xrel, int yrel)
{
if (holdleft) // Rotate
{
//printf("Rotate\n");
angleZ += event.xrel*rotateSensivity;
angleY += event.yrel*rotateSensivity;
angleZ += xrel*rotateSensivity;
angleY += yrel*rotateSensivity;

if (angleY > 90)
angleY = 90;
else if (angleY < -90)
angleY = -90;
if (angleY > 90)
angleY = 90;
else if (angleY < -90)
angleY = -90;

changed = true;
}
else if (holdright) // Pan
{
panSensivity = distance*0.01;
//printf("Pan\n");
deltaX += event.xrel*panSensivity;
deltaY -= event.yrel*panSensivity;
changed = true;
}
changed = true;
}

void Camera::OnMouseEvent(const SDL_MouseButtonEvent &event, const SDL_MouseWheelEvent &event_wheel)
void Camera::pan(int xrel, int yrel)
{
//printf("Mouse event\n");
panSensivity = distance*0.01;
deltaX += xrel*panSensivity;
deltaY -= yrel*panSensivity;
changed = true;
}

if (event.button == SDL_BUTTON_LEFT)
{
if ((holdleft) && (event.type == SDL_MOUSEBUTTONUP))
{
holdleft = false;
SDL_SetCursor(_hand1);
}
else if ((!holdleft) && (event.type == SDL_MOUSEBUTTONDOWN))
{
holdleft = true;
SDL_SetCursor(_hand2);
}
}
else if (event.button == SDL_BUTTON_RIGHT)
void Camera::zoom(int zrel)
{
if (zrel > 0)
{
if ((holdright) && (event.type == SDL_MOUSEBUTTONUP))
{
holdright = false;
SDL_SetCursor(_hand1);
}
else if ((!holdright) && (event.type == SDL_MOUSEBUTTONDOWN))
{
holdright = true;
SDL_SetCursor(_move);
}
distance += zoomSensivity;
panSensivity = distance * 0.001;
zoomSensivity = distance * 0.05;
}

// Handle mouse wheel event separately
if (event_wheel.type == SDL_MOUSEWHEEL)
else if (zrel < 0)
{
//printf("Wheel even\n");

if (event_wheel.y > 0)
{
//printf("Wheel up\n");
distance += zoomSensivity;
panSensivity = distance * 0.001;
zoomSensivity = distance * 0.05;
changed = true;
}
else if (event_wheel.y < 0)
{
//printf("Wheel down\n");
distance -= zoomSensivity;
panSensivity = distance * 0.001;
zoomSensivity = distance * 0.05;
changed = true;
}
distance -= zoomSensivity;
panSensivity = distance * 0.001;
zoomSensivity = distance * 0.05;
}
}

void Camera::OnKeyboard(const SDL_KeyboardEvent & event)
{
if ((event.type == SDL_KEYDOWN)&&(event.keysym.sym == SDLK_HOME))
{
angleY = 0;
angleZ = 0;
changed = true;
}
changed = true;
}

void Camera::setPanSensivity(double sensivity)
Expand Down Expand Up @@ -360,13 +215,6 @@ void Camera::setDistance(double dist)
distance = dist;
}

Camera::~Camera()
{
SDL_FreeCursor(_hand1);
SDL_FreeCursor(_hand2);
SDL_SetCursor(NULL);
}

void Camera::look()
{
glTranslated(deltaX, deltaY, 0.0);
Expand All @@ -377,6 +225,7 @@ void Camera::look()

frustum.CalculateFrustum();

// Get the camera position
GLfloat viewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);

Expand Down
14 changes: 3 additions & 11 deletions src/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
#define CAMERA_H

#include "Frustum.h"
#include <SDL2/SDL.h>

class Camera
{
public:
Camera();
~Camera();

void OnMouseMotion(const SDL_MouseMotionEvent & event);
void OnMouseEvent(const SDL_MouseButtonEvent & event, const SDL_MouseWheelEvent & event_wheel);
void OnKeyboard(const SDL_KeyboardEvent & event);

void rotate(int xrel, int yrel);
void pan(int xrel, int yrel);
void zoom(int zrel);
void look();
void setRotateSensivity(double sensivity);
void setPanSensivity(double sensivity);
Expand All @@ -36,11 +33,6 @@ class Camera
double deltaX;
double deltaY;
double deltaZ;
bool holdleft;
bool holdright;
SDL_Cursor * _hand1;
SDL_Cursor * _hand2;
SDL_Cursor * _move;

private:
CFrustum frustum;
Expand Down
Loading

0 comments on commit c266527

Please sign in to comment.