Skip to content

Commit

Permalink
Merge pull request #3 from sariug/clipping_as_a_new_file
Browse files Browse the repository at this point in the history
Clipping as a new file
  • Loading branch information
sariug committed Mar 9, 2020
2 parents 7a064aa + db174db commit 871c05d
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 83 deletions.
143 changes: 143 additions & 0 deletions src/clipping.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/* Goxel 3D voxels editor
*
* copyright (c) 2017 Guillaume Chereau <guillaume@noctua-software.com>
*
* Goxel is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
* Goxel is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* goxel. If not, see <http://www.gnu.org/licenses/>.
*/

#include "goxel.h"

void clip_init(void)
{
goxel.clipping.clip=true;

float bbox[4][4];
box_get_bbox(goxel.image->active_layer->box,bbox);
goxel.clipping.origin[0] = bbox[3][0];
goxel.clipping.origin[1] = bbox[3][1];
goxel.clipping.origin[2] = bbox[3][2];
}
void clip_cancel()
{
goxel.clipping.clip=false;
}

static layer_t *clip_layer(image_t *img, layer_t *layer)
{
layer_t *clipped_layer;

img = img?:goxel.image;
layer = layer?:img->active_layer;
layer->visible=false; // Hide the clipped layer

clipped_layer = image_duplicate_layer(img,layer);


mesh_iterator_t iter;
mesh_t *mesh2clip = clipped_layer->mesh;
mesh_accessor_t accessor;
int vp[3];
float dot, p[3], p_vec[3];
uint8_t new_value[4];
for(int i = 0;i<4;i++)
new_value[i]=0;
iter = mesh_get_iterator(mesh2clip,MESH_ITER_VOXELS);

accessor = mesh_get_accessor(mesh2clip);
while (mesh_iter(&iter, vp)) {
vec3_set(p, vp[0] + 0.5, vp[1] + 0.5, vp[2] + 0.5);
for(int i = 0;i<3;i++)
p_vec[i]=p[i]- goxel.clipping.origin[i];
vec3_normalize(p_vec,p_vec);
dot = vec3_dot(p_vec, goxel.clipping.normal);
if (dot>0)
mesh_set_at(mesh2clip, &accessor, vp,new_value);
}
goxel.clipping.clip=false;
// Update name
strcat(clipped_layer->name,"_clipped");
return clipped_layer;
}

static void clipping_normal_x()
{
vec3_set(goxel.clipping.normal, 1, 0, 0);
}
static void clipping_normal_y()
{
vec3_set(goxel.clipping.normal, 0, 1, 0);
}
static void clipping_normal_z()
{
vec3_set(goxel.clipping.normal, 0, 0, 1);
}
static void clipping_normal_cam()
{
camera_t *camera = goxel.image->active_camera;
float cam_normal[3];
vec3_sub(camera->mat[3],goxel.clipping.origin, cam_normal);
vec3_normalize(cam_normal,goxel.clipping.normal);
}
static void clipping_invert_normal()
{
vec3_mul(goxel.clipping.normal,-1,goxel.clipping.normal);
}
ACTION_REGISTER(clipping_invert_normal,
.help = "Cliping normal is being inverted.",
.cfunc = clipping_invert_normal,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
ACTION_REGISTER(clipping_normal_x,
.help = "Cliping normal is X axis",
.cfunc = clipping_normal_x,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
ACTION_REGISTER(clipping_normal_y,
.help = "Cliping normal is Y axis",
.cfunc = clipping_normal_y,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
ACTION_REGISTER(clipping_normal_z,
.help = "Cliping normal is Z axis",
.cfunc = clipping_normal_z,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
ACTION_REGISTER(clipping_normal_cam,
.help = "Cliping normal is Camera direction",
.cfunc = clipping_normal_cam,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
ACTION_REGISTER(clip_init,
.help = "Start clipping the current layer",
.cfunc = clip_init,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
ACTION_REGISTER(clip_layer,
.help = "Clip the current layer",
.cfunc = clip_layer,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
ACTION_REGISTER(clip_cancel,
.help = "Cancel clipping",
.cfunc = clip_cancel,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)
55 changes: 13 additions & 42 deletions src/goxel.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,9 +900,21 @@ void goxel_render_view(const float viewport[4], bool render_mode)
if (goxel.clipping.clip && !mesh_is_empty(goxel.image->active_layer->mesh))
{
float b[4][4];
bbox_grow(goxel.image->active_layer->box,5,5,5,b);
float end[3], box_size[3], max_length;
box_get_size(goxel.image->active_layer->box, box_size);
bbox_grow(goxel.image->active_layer->box,box_size[0]/10,
box_size[1]/10,
box_size[2]/10,b);
render_grid(rend, goxel.clipping.plane,
goxel.clipping.color,b);

// Render clipping direction pointer
max_length = max(box_size[0], max(box_size[1],box_size[2]))*1.25;
for(int i = 0;i<3;i++)
end[i]=goxel.clipping.origin[i]+goxel.clipping.normal[i]*max_length;
uint8_t c[4];
vec4_set(c, 255, 0, 0, 255);
render_line(rend, goxel.clipping.origin, end,c,0);
}
if (goxel.show_export_viewport)
render_export_viewport(viewport);
Expand Down Expand Up @@ -1152,47 +1164,6 @@ ACTION_REGISTER(export,
.csig = "vp",
)

static layer_t *clip_layer(image_t *img, layer_t *layer)
{
layer_t *clipped_layer;

img = img?:goxel.image;
layer = layer?:img->active_layer;
layer->visible=false; // Hide the clipped layer
clipped_layer = image_duplicate_layer(img,layer);


mesh_iterator_t iter;
mesh_t *mesh2clip = clipped_layer->mesh;
mesh_accessor_t accessor;
int vp[3];
float dot;
float p[3], p_vec[3];
uint8_t new_value[4];
for(int i = 0;i<4;i++)
new_value[i]=0;
iter = mesh_get_iterator(mesh2clip,MESH_ITER_VOXELS);

accessor = mesh_get_accessor(mesh2clip);
while (mesh_iter(&iter, vp)) {
vec3_set(p, vp[0] + 0.5, vp[1] + 0.5, vp[2] + 0.5);
for(int i = 0;i<3;i++)
p_vec[i]=p[i]- goxel.clipping.origin[i];
vec3_normalize(p_vec,p_vec);
dot = vec3_dot(p_vec, goxel.clipping.normal);
if (dot>0)
mesh_set_at(mesh2clip, &accessor, vp,new_value);
}
return clipped_layer;
}

ACTION_REGISTER(clip_layer,
.help = "Clip the current layer",
.cfunc = clip_layer,
.csig = "vpp",
.flags = ACTION_TOUCH_IMAGE,
)

static layer_t *cut_as_new_layer(image_t *img, layer_t *layer,
const float box[4][4])
{
Expand Down
25 changes: 25 additions & 0 deletions src/gui/layers_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void gui_layers_panel(void)
gui_group_begin(NULL);
gui_action_button("img_duplicate_layer", "Duplicate", 1, "");
gui_action_button("img_clone_layer", "Clone", 1, "");
gui_action_button("clip_init", "Clip", 1, "");
gui_action_button("img_merge_visible_layers", "Merge visible", 1, "");

layer = goxel.image->active_layer;
Expand Down Expand Up @@ -129,4 +130,28 @@ void gui_layers_panel(void)
}
gui_combo_end();
}

if(goxel.clipping.clip) {
if(gui_collapsing_header("Clipping Parameters",false))
{
gui_vector_float("Origin", goxel.clipping.origin, 0.1, .0f, .0f, NULL);
gui_vector_float("Normal", goxel.clipping.normal, 0.1, -1.0f, 1.0f, NULL);

gui_action_button("clipping_normal_x", "Normal X", .5, "");
gui_same_line();
gui_action_button("clipping_normal_y", "Normal Y", 1, "");
gui_action_button("clipping_normal_z", "Normal Z", .5, "");
gui_same_line();
gui_action_button("clipping_normal_cam", "Normal Cam", 1, "");

gui_action_button("clipping_invert_normal", "Invert Normal", 1, "");
}
gui_action_button("clip_layer", "Apply Clipping", 1, "");
gui_action_button("clip_cancel", "Cancel", 1, "");

// Update Plane
plane_from_normal(goxel.clipping.plane,goxel.clipping.origin,goxel.clipping.normal);

}

}
41 changes: 0 additions & 41 deletions src/gui/view_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,45 +65,4 @@ void gui_view_panel(void)
EFFECT_MC_SMOOTH, NULL);
}

gui_checkbox("Clip", &goxel.clipping.clip, NULL);
if(goxel.clipping.clip) {
gui_vector_float("Origin", goxel.clipping.origin, 0.1, .0f, .0f, NULL);
gui_vector_float("Normal", goxel.clipping.normal, 0.1, -1.0f, 1.0f, NULL);
if(gui_button("Normal X", .5, 0))
{
goxel.clipping.normal[0] = 1;
goxel.clipping.normal[1] = 0;
goxel.clipping.normal[2] = 0;
}
gui_same_line();
if(gui_button("Normal Y", 1, 0))
{
goxel.clipping.normal[0] = 0;
goxel.clipping.normal[1] = 1;
goxel.clipping.normal[2] = 0;
}
if(gui_button("Normal Z", .5, 0))
{
goxel.clipping.normal[0] = 0;
goxel.clipping.normal[1] = 0;
goxel.clipping.normal[2] = 1;
} gui_same_line();
if(gui_button("Normal Cam", 1, 0))
{
// camera_t *camera = goxel.image->active_camera;
// camera->view_mat;
// goxel.image->active_layer->box;
// goxel.clipping.normal=goxel.
}
if(gui_button("Invert Normal", 1, 0))
{
for(int i = 0; i<3;i++)
goxel.clipping.normal[i] = -goxel.clipping.normal[i];
}
gui_action_button("clip_layer", "Clip", 1, "");

// Update Plane
plane_from_normal(goxel.clipping.plane,goxel.clipping.origin,goxel.clipping.normal);
}

}

0 comments on commit 871c05d

Please sign in to comment.