New curve type : "cage control"

This commit is contained in:
Hombre
2010-07-14 22:57:47 +02:00
parent f16761ac95
commit a645b0a804
10 changed files with 385 additions and 124 deletions

View File

@@ -24,7 +24,7 @@
extern Glib::ustring argv0;
CurveEditor::CurveEditor () : cl(NULL), activeParamControl(-1), realized(false), curveTypeIx(-1) {
CurveEditor::CurveEditor () : cl(NULL), activeParamControl(-1), realized(false), curveTypeIx(Linear) {
Gtk::HBox* tsbox = Gtk::manage (new Gtk::HBox ());
Gtk::Label* tslab = Gtk::manage (new Gtk::Label (M("CURVEEDITOR_TYPE")));
@@ -40,11 +40,13 @@ CurveEditor::CurveEditor () : cl(NULL), activeParamControl(-1), realized(false),
tsbox->pack_start (*curve_reset, Gtk::PACK_SHRINK, 0);
pack_start (*tsbox);
curveType->append_text (M("CURVEEDITOR_LINEAR"));
curveType->append_text (M("CURVEEDITOR_PARAMETRIC"));
curveType->append_text (M("CURVEEDITOR_CUSTOM"));
curveType->set_active (0);
// Order set in the same order than "enum CurveType". Shouldn't change, for compatibility reason
curveType->append_text (M("CURVEEDITOR_LINEAR")); // 0 Linear
curveType->append_text (M("CURVEEDITOR_CUSTOM")); // 1 Spline
curveType->append_text (M("CURVEEDITOR_PARAMETRIC")); // 2 Parametric
curveType->append_text (M("CURVEEDITOR_NURBS")); // 3 NURBS
curveType->set_active (Linear);
curve_reset->signal_clicked().connect( sigc::mem_fun(*this, &CurveEditor::curveResetPressed) );
@@ -52,7 +54,6 @@ CurveEditor::CurveEditor () : cl(NULL), activeParamControl(-1), realized(false),
customCurveBox = new Gtk::VBox ();
Gtk::HBox* tmpa = Gtk::manage (new Gtk::HBox ());
customCurve = Gtk::manage (new MyCurve ());
Gtk::Table* cctab = Gtk::manage (new Gtk::Table (2,1));
//Gtk::AspectFrame* af = Gtk::manage (new Gtk::AspectFrame ("",Gtk::ALIGN_CENTER,Gtk::ALIGN_CENTER,1,false));
//af->add (*customCurve);
customCurve->set_size_request (GRAPH_SIZE+2*RADIUS, GRAPH_SIZE+2*RADIUS);
@@ -61,22 +62,51 @@ CurveEditor::CurveEditor () : cl(NULL), activeParamControl(-1), realized(false),
tmpa->pack_start (*customCurve, true, false, 4);
customCurveBox->pack_start (*tmpa, true, true,4);
Gtk::HBox* bbox = Gtk::manage (new Gtk::HBox ());
save = Gtk::manage (new Gtk::Button ());
save->add (*Gtk::manage (new Gtk::Image (Gtk::StockID("gtk-save"), Gtk::ICON_SIZE_BUTTON)));
load = Gtk::manage (new Gtk::Button ());
load->add (*Gtk::manage (new Gtk::Image (Gtk::StockID("gtk-open"), Gtk::ICON_SIZE_BUTTON)));
Gtk::HBox* custombbox = Gtk::manage (new Gtk::HBox ());
saveCustom = Gtk::manage (new Gtk::Button ());
saveCustom->add (*Gtk::manage (new Gtk::Image (Gtk::StockID("gtk-save"), Gtk::ICON_SIZE_BUTTON)));
loadCustom = Gtk::manage (new Gtk::Button ());
loadCustom->add (*Gtk::manage (new Gtk::Image (Gtk::StockID("gtk-open"), Gtk::ICON_SIZE_BUTTON)));
bbox->pack_end (*save, Gtk::PACK_EXPAND_WIDGET, 4);
bbox->pack_end (*load, Gtk::PACK_EXPAND_WIDGET, 4);
custombbox->pack_end (*saveCustom, Gtk::PACK_EXPAND_WIDGET, 4);
custombbox->pack_end (*loadCustom, Gtk::PACK_EXPAND_WIDGET, 4);
customCurveBox->pack_end (*bbox, Gtk::PACK_SHRINK, 2);
customCurveBox->pack_end (*custombbox, Gtk::PACK_SHRINK, 2);
customCurveBox->show_all ();
save->signal_clicked().connect( sigc::mem_fun(*this, &CurveEditor::savePressed) );
load->signal_clicked().connect( sigc::mem_fun(*this, &CurveEditor::loadPressed) );
save->set_tooltip_text (M("CURVEEDITOR_TOOLTIPSAVE"));
load->set_tooltip_text (M("CURVEEDITOR_TOOLTIPLOAD"));
saveCustom->signal_clicked().connect( sigc::mem_fun(*this, &CurveEditor::savePressed) );
loadCustom->signal_clicked().connect( sigc::mem_fun(*this, &CurveEditor::loadPressed) );
saveCustom->set_tooltip_text (M("CURVEEDITOR_TOOLTIPSAVE"));
loadCustom->set_tooltip_text (M("CURVEEDITOR_TOOLTIPLOAD"));
// NURBS curve
NURBSCurveBox = new Gtk::VBox ();
Gtk::HBox* tmpb = Gtk::manage (new Gtk::HBox ());
NURBSCurve = Gtk::manage (new MyCurve ());
//Gtk::AspectFrame* af = Gtk::manage (new Gtk::AspectFrame ("",Gtk::ALIGN_CENTER,Gtk::ALIGN_CENTER,1,false));
//af->add (*customCurve);
NURBSCurve->set_size_request (GRAPH_SIZE+2*RADIUS, GRAPH_SIZE+2*RADIUS);
NURBSCurve->setType (NURBS);
//customCurve->set_tooltip_text (M("CURVEEDITOR_TOOLTIPMOVESPEED"));
tmpb->pack_start (*NURBSCurve, true, false, 4);
NURBSCurveBox->pack_start (*tmpb, true, true,4);
Gtk::HBox* NURBSbbox = Gtk::manage (new Gtk::HBox ());
saveNURBS = Gtk::manage (new Gtk::Button ());
saveNURBS->add (*Gtk::manage (new Gtk::Image (Gtk::StockID("gtk-save"), Gtk::ICON_SIZE_BUTTON)));
loadNURBS = Gtk::manage (new Gtk::Button ());
loadNURBS->add (*Gtk::manage (new Gtk::Image (Gtk::StockID("gtk-open"), Gtk::ICON_SIZE_BUTTON)));
NURBSbbox->pack_end (*saveNURBS, Gtk::PACK_EXPAND_WIDGET, 4);
NURBSbbox->pack_end (*loadNURBS, Gtk::PACK_EXPAND_WIDGET, 4);
NURBSCurveBox->pack_end (*NURBSbbox, Gtk::PACK_SHRINK, 2);
NURBSCurveBox->show_all ();
saveNURBS->signal_clicked().connect( sigc::mem_fun(*this, &CurveEditor::savePressed) );
loadNURBS->signal_clicked().connect( sigc::mem_fun(*this, &CurveEditor::loadPressed) );
saveNURBS->set_tooltip_text (M("CURVEEDITOR_TOOLTIPSAVE"));
loadNURBS->set_tooltip_text (M("CURVEEDITOR_TOOLTIPLOAD"));
// parametric curve
paramCurveBox = new Gtk::VBox ();
@@ -92,10 +122,10 @@ CurveEditor::CurveEditor () : cl(NULL), activeParamControl(-1), realized(false),
paramctab->attach (*paramCurve, 0, 1, 0, 1, Gtk::FILL, Gtk::SHRINK, 4, 4);
paramctab->attach (*shcSelector, 0, 1, 1, 2, Gtk::FILL, Gtk::SHRINK, RADIUS+4, 0);
Gtk::HBox* tmpb = Gtk::manage (new Gtk::HBox ());
tmpb->pack_start (*paramctab, true, false);
Gtk::HBox* tmpc = Gtk::manage (new Gtk::HBox ());
tmpc->pack_start (*paramctab, true, false);
paramCurveBox->pack_start (*tmpb, true, true);
paramCurveBox->pack_start (*tmpc, true, true);
highlights = Gtk::manage (new Adjuster (M("CURVEEDITOR_HIGHLIGHTS"), -100, 100, 1, 0));
lights = Gtk::manage (new Adjuster (M("CURVEEDITOR_LIGHTS"), -100, 100, 1, 0));
@@ -123,6 +153,7 @@ CurveEditor::CurveEditor () : cl(NULL), activeParamControl(-1), realized(false),
paramCurveBox->reference ();
customCurve->setCurveListener (this);
NURBSCurve->setCurveListener (this);
paramCurve->setCurveListener (this);
shcSelector->setSHCListener (this);
@@ -196,11 +227,26 @@ void CurveEditor::savePressed () {
std::ofstream f (fname.c_str());
std::vector<double> p = customCurve->getPoints ();
switch (curveType->get_active_row_number()) {
case Spline: // custom
p = customCurve->getPoints ();
break;
case NURBS: // NURBS
p = NURBSCurve->getPoints ();
break;
default:
break;
}
int ix = 0;
if (p[ix++]<0)
if (p[ix]==(double)(Linear))
f << "Linear\n";
else
else if (p[ix]==(double)(Spline))
f << "Spline\n";
else if (p[ix]==(double)(NURBS))
f << "NURBS\n";
ix++;
for (int i=0; i<p.size()/2; i++, ix+=2)
f << p[ix] << ' ' << p[ix+1] << std::endl;
f.close ();
@@ -233,9 +279,11 @@ void CurveEditor::loadPressed () {
std::string s;
f >> s;
if (s=="Linear")
p.push_back (-1);
p.push_back ((double)(Linear));
else if (s=="Spline")
p.push_back (1);
p.push_back ((double)(Spline));
else if (s=="NURBS")
p.push_back ((double)(NURBS));
else return;
double x;
while (f) {
@@ -243,9 +291,16 @@ void CurveEditor::loadPressed () {
if (f)
p.push_back (x);
}
customCurve->setPoints (p);
customCurve->queue_draw ();
customCurve->notifyListener ();
if (p[0] == (double)(Spline)) {
customCurve->setPoints (p);
customCurve->queue_draw ();
customCurve->notifyListener ();
}
else if (p[0] == (double)(NURBS)) {
NURBSCurve->setPoints (p);
NURBSCurve->queue_draw ();
NURBSCurve->notifyListener ();
}
}
}
}
@@ -261,21 +316,21 @@ void CurveEditor::setCurve (const std::vector<double>& c) {
tmpCurve = c;
if (realized && curveType->get_active_row_number()<3) { // if it is not realized or "unchanged" is selected, just store the curve (prev line) and do not change gui
if (realized && curveType->get_active_row_number()<=Unchanged) { // if it is not realized or "unchanged" is selected, just store the curve (prev line) and do not change gui
typeconn.block(true);
if (c.size()==0 || c[0]==0) {
curveType->set_active (0);
curveTypeIx = 0;
if (c.size()==0 || c[0]==(double)(Linear)) {
curveType->set_active (Linear); // Change the combo selection
curveTypeIx = Linear;
}
else if (c[0]==1) {
curveType->set_active (2);
curveTypeIx = 2;
else if (c[0]==(double)(Spline)) {
curveType->set_active (Spline);
curveTypeIx = Spline;
customCurve->setPoints (c);
}
else if (c[0]==2) {
curveType->set_active (1);
curveTypeIx = 1;
else if (c[0]==(double)(Parametric)) {
curveType->set_active (Parametric);
curveTypeIx = Parametric;
paramCurve->setPoints (c);
shcSelector->setPositions (c[1], c[2], c[3]);
highlights->setValue (c[4]);
@@ -283,13 +338,21 @@ void CurveEditor::setCurve (const std::vector<double>& c) {
darks->setValue (c[6]);
shadows->setValue (c[7]);
}
else if (c[0]==(double)(NURBS)) {
curveType->set_active (NURBS);
curveTypeIx = NURBS;
NURBSCurve->setPoints (c);
}
removeIfThere (this, customCurveBox, false);
removeIfThere (this, paramCurveBox, false);
removeIfThere (this, NURBSCurveBox, false);
if (curveType->get_active_row_number()==1)
pack_start (*paramCurveBox);
else if (curveType->get_active_row_number()==2)
if (curveType->get_active_row_number()==Spline)
pack_start (*customCurveBox);
else if (curveType->get_active_row_number()==Parametric)
pack_start (*paramCurveBox);
else if (curveType->get_active_row_number()==NURBS)
pack_start (*NURBSCurveBox);
typeconn.block(false);
}
@@ -297,17 +360,19 @@ void CurveEditor::setCurve (const std::vector<double>& c) {
std::vector<double> CurveEditor::getCurve () {
if (!realized || curveType->get_active_row_number()==3)
if (!realized || curveType->get_active_row_number()==Unchanged)
return tmpCurve;
if (curveTypeIx<=0) {
// linear
if (curveTypeIx<=Linear) {
std::vector<double> lcurve (1);
lcurve[0] = 0.0;
lcurve[0] = (double)(Linear);
return lcurve;
}
else if (curveTypeIx==1) {
// parametric
else if (curveTypeIx==Parametric) {
std::vector<double> lcurve (8);
lcurve[0] = 2.0;
lcurve[0] = (double)(Parametric);
shcSelector->getPositions (lcurve[1], lcurve[2], lcurve[3]);
lcurve[4] = highlights->getValue ();
lcurve[5] = lights->getValue ();
@@ -315,23 +380,33 @@ std::vector<double> CurveEditor::getCurve () {
lcurve[7] = shadows->getValue ();
return lcurve;
}
else if (curveTypeIx==2)
// spline (custom)
else if (curveTypeIx==Spline)
return customCurve->getPoints ();
// NURBS (control cage)
else if (curveTypeIx==NURBS)
return NURBSCurve->getPoints ();
}
void CurveEditor::typeSelectionChanged () {
removeIfThere (this, customCurveBox, false);
removeIfThere (this, paramCurveBox, false);
removeIfThere (this, NURBSCurveBox, false);
if (curveType->get_active_row_number()==1)
pack_start (*paramCurveBox);
else if (curveType->get_active_row_number()==2)
if (curveType->get_active_row_number()==Spline) {
pack_start (*customCurveBox);
}
else if (curveType->get_active_row_number()==Parametric) {
pack_start (*paramCurveBox);
}
else if (curveType->get_active_row_number()==NURBS) {
pack_start (*NURBSCurveBox);
}
if (curveType->get_active_row_number() < Unchanged)
curveTypeIx = (CurveType)curveType->get_active_row_number();
if (curveType->get_active_row_number()<3)
curveTypeIx = curveType->get_active_row_number();
curveChanged ();
}
@@ -343,10 +418,13 @@ void CurveEditor::curveChanged () {
void CurveEditor::curveResetPressed () {
switch (curveTypeIx) {
case 2 : // Custom
case NURBS : // = Control cage
NURBSCurve->reset ();
break;
case Spline : // = Custom
customCurve->reset ();
break;
case 1 : // Parametric
case Parametric :
highlights->resetPressed();
lights->resetPressed();
darks->resetPressed();
@@ -399,7 +477,7 @@ void CurveEditor::setBatchMode (bool batchMode) {
bool CurveEditor::isUnChanged () {
return curveType->get_active_row_number()==3;
return curveType->get_active_row_number()==Unchanged;
}
void CurveEditor::setUnChanged (bool uc) {
@@ -408,7 +486,8 @@ void CurveEditor::setUnChanged (bool uc) {
typeconn.block(true);
removeIfThere (this, customCurveBox, false);
removeIfThere (this, paramCurveBox, false);
curveType->set_active (3);
removeIfThere (this, NURBSCurveBox, false);
curveType->set_active (Unchanged);
typeconn.block(false);
}
else {
@@ -423,4 +502,5 @@ void CurveEditor::updateBackgroundHistogram (unsigned int* hist) {
paramCurve->updateBackgroundHistogram (hist);
customCurve->updateBackgroundHistogram (hist);
NURBSCurve->updateBackgroundHistogram (hist);
}

View File

@@ -30,27 +30,31 @@ class CurveEditor : public Gtk::VBox, public CurveListener, public SHCListener,
Gtk::Button* curve_reset;
Gtk::VBox* paramCurveBox;
Gtk::VBox* customCurveBox;
Gtk::VBox* NURBSCurveBox;
MyCurve* customCurve;
MyCurve* NURBSCurve;
MyCurve* paramCurve;
SHCSelector* shcSelector;
Adjuster* highlights;
Adjuster* lights;
Adjuster* darks;
Adjuster* shadows;
Gtk::Button* save;
Gtk::Button* load;
Gtk::Button* saveCustom;
Gtk::Button* loadCustom;
Gtk::Button* saveNURBS;
Gtk::Button* loadNURBS;
CurveListener* cl;
bool realized;
std::vector<double> tmpCurve;
int curveTypeIx;
CurveType curveTypeIx;
int activeParamControl;
sigc::connection typeconn;
public:

View File

@@ -56,9 +56,9 @@ std::vector<double> MyCurve::get_vector (int veclen) {
std::vector<double> vector;
vector.resize (veclen);
if (curve.type != Parametric) {
// count active points:
// count active points:
double prev =- 1.0;
int active = 0;
int firstact = -1;
@@ -69,7 +69,7 @@ std::vector<double> MyCurve::get_vector (int veclen) {
prev = curve.x[i];
++active;
}
// handle degenerate case:
// handle degenerate case:
if (active < 2) {
double ry;
if (active > 0)
@@ -86,7 +86,7 @@ std::vector<double> MyCurve::get_vector (int veclen) {
// calculate remaining points
std::vector<double> curveDescr = getPoints ();
rtengine::Curve* rtcurve = new rtengine::Curve (curveDescr);
rtengine::Curve* rtcurve = new rtengine::Curve (curveDescr, veclen*1.5);
std::vector<double> t;
t.resize (veclen);
for (int i = 0; i < veclen; i++)
@@ -211,6 +211,25 @@ void MyCurve::draw (int width, int height, int handle) {
cr->fill ();
}
// draw the cage of the NURBS curve
if (curve.type==NURBS) {
std::valarray<double> ch_ds (1);
ch_ds[0] = 2;
cr->set_dash (ch_ds, 0);
cr->set_source_rgb (0.0, 0.0, 0.0);
std::vector<double> points = getPoints();
for (int i = 1; i < points.size(); ) {
double x = ((width-1) * points[i++] + 0.5)+RADIUS; // project (curve.x[i], 0, 1, width);
double y = height - ((height-1) * points[i++] + 0.5)+RADIUS; // project (curve.y[i], 0, 1, height);
if (i==3)
cr->move_to (x, y);
else
cr->line_to (x, y);
}
cr->stroke ();
cr->unset_dash ();
}
// draw curve
cr->set_source_rgb (0.0, 0.0, 0.0);
cr->move_to (point[0].get_x(), point[0].get_y());
@@ -258,17 +277,26 @@ bool MyCurve::handleEvents (GdkEvent* event) {
int x = CLAMP ((tx - RADIUS), 0, width-1); // X position of the pointer from the origin of the graph
int y = height-1 - CLAMP ((ty - RADIUS), 0, height-1); // Y position of the pointer from the origin of the graph
unsigned int distance = ~0U;
unsigned int distance_x = ~0U, distance_y = ~0U;
int num = curve.x.size();
int closest_point = -1;
if (curve.type!=Parametric) {
for (int i = 0; i < num; ++i) {
int cx = (int)((width-1) * curve.x[i] + 0.5); //project (c->ctlpoint[i][0], min_x, c->max_x, width);
if ((unsigned int) abs (x - cx) < distance) {
distance = abs (x - cx);
int cy = (int)((height-1) * curve.y[i] + 0.5); //project (c->ctlpoint[i][0], min_x, c->max_x, width);
unsigned int curr_dist_x = abs (x - cx);
unsigned int curr_dist_y = abs (y - cy);
if (curr_dist_x < distance_x) {
distance_x = curr_dist_x;
distance_y = curr_dist_y;
closest_point = i;
}
else if (curr_dist_x == distance_x && curr_dist_y < distance_y) {
// there is mode than 1 point for that X coordinate, we select the point closest to the cursor
distance_y = curr_dist_y;
closest_point = i;
}
}
}
@@ -295,7 +323,7 @@ bool MyCurve::handleEvents (GdkEvent* event) {
rt_display->get_pointer(cursor_x, cursor_y, mod_type);
new_type = CSEmpty;
if (distance > MIN_DISTANCE) {
if (distance_x > MIN_DISTANCE) {
/* insert a new control point */
if (num > 0) {
int cx = (int)((width-1)*curve.x[closest_point]+0.5);
@@ -350,7 +378,7 @@ bool MyCurve::handleEvents (GdkEvent* event) {
draw (width, height, lit_point);
}
}
if (distance <= MIN_DISTANCE) {
if (distance_x <= MIN_DISTANCE) {
new_type = CSMove;
lit_point = closest_point;
}
@@ -376,11 +404,11 @@ bool MyCurve::handleEvents (GdkEvent* event) {
case Gdk::MOTION_NOTIFY:
mevent = (GdkEventMotion *) event;
if (curve.type == Linear || curve.type == Spline) {
if (curve.type == Linear || curve.type == Spline || curve.type == NURBS) {
if (grab_point == -1) {
int previous_lit_point = lit_point;
/* if no point is grabbed... */
if (distance <= MIN_DISTANCE) {
if (distance_x <= MIN_DISTANCE) {
new_type = CSMove;
lit_point = closest_point;
}
@@ -501,15 +529,19 @@ std::vector<double> MyCurve::getPoints () {
std::vector<double> result;
if (curve.type==Parametric) {
result.push_back (+2.0);
result.push_back ((double)(Parametric));
for (int i=0; i<curve.x.size(); i++)
result.push_back (curve.x[i]);
}
else {
// the first value gives the type of the curve
if (curve.type==Linear)
result.push_back (-1.0);
else
result.push_back (+1.0);
result.push_back ((double)(Linear));
else if (curve.type==Spline)
result.push_back ((double)(Spline));
else if (curve.type==NURBS)
result.push_back ((double)(NURBS));
// then we push all the points coordinate
for (int i=0; i<curve.x.size(); i++)
if (curve.x[i]>=0) {
result.push_back (curve.x[i]);
@@ -522,19 +554,15 @@ std::vector<double> MyCurve::getPoints () {
void MyCurve::setPoints (const std::vector<double>& p) {
int ix = 0;
int t = p[ix++];
if (t==2) {
curve.type = Parametric;
CurveType t = (CurveType)p[ix++];
curve.type = t;
if (t==Parametric) {
curve.x.clear ();
curve.y.clear ();
for (int i=1; i<p.size(); i++)
curve.x.push_back (p[ix++]);
}
else {
if (t==1)
curve.type = Spline;
else
curve.type = Linear;
curve.x.clear ();
curve.y.clear ();
for (int i=0; i<p.size()/2; i++) {
@@ -609,6 +637,7 @@ void MyCurve::reset() {
switch (curve.type) {
case Spline :
case NURBS :
curve.x.clear();
curve.y.clear();
curve.x.push_back(0.);
@@ -624,5 +653,5 @@ void MyCurve::reset() {
default:
break;
}
//draw(width, height, -1);
draw(width, height, -1);
}

View File

@@ -28,7 +28,16 @@
#define MIN_DISTANCE 8 /* min distance between control points */
#define GRAPH_SIZE 200 /* size of the curve editor graphic */
enum CurveType {Linear, Spline, Parametric};
// For compatibility and simplicity reason, order shouldn't change, and must be identical to the order specified in the curveType widget
enum CurveType {
Empty = -1,
Linear, // 0
Spline, // 1
Parametric, // 2
NURBS, // 3
// Insert new curve type above this line
Unchanged // Must remain the last of the enum
};
class CurveDescr {
@@ -65,7 +74,7 @@ class MyCurve : public Gtk::DrawingArea {
bool bghistvalid;
MyCurveIdleHelper* mcih;
int cursor_x, cursor_y;
double ugp_x, ugp_y; // unclamped grabed point coordinates
double ugp_x, ugp_y; // unclamped grabbed point coordinates
void draw (int width, int height, int handle);
void interpolate (int width, int height);