Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

chore: update CMakeLists.txt to use C++17

Anderson Prado7803d719be87
diff --git a/toonz/sources/CMakeLists.txt b/toonz/sources/CMakeLists.txt
index ef1384307..2f4280ac9 100644
--- a/toonz/sources/CMakeLists.txt
+++ b/toonz/sources/CMakeLists.txt
@@ -1,9 +1,13 @@
-cmake_minimum_required(VERSION 3.10)
+cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_BUILD_TYPE_INIT Release)
project(OpenToonz)
+# Set C++17 globally
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
#-----------------------------------------------------------------------------
# Platform Specific Defaults
@@ -171,8 +175,8 @@ if(BUILD_ENV_MSVC)
endif()
set(QT_LIB_PATH ${QT_PATH})
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${QT_PATH}/lib/cmake/")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4251")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251")
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /std:c++17 /wd4251")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /wd4251")
add_definitions(
-DVC_EXTRALEAN
-DNOMINMAX
@@ -202,7 +206,7 @@ elseif(BUILD_ENV_APPLE)
endif()
elseif(BUILD_ENV_UNIXLIKE)
# Needed for correct Qt detection
- cmake_minimum_required(VERSION 3.10)
+ cmake_minimum_required(VERSION 2.8.12)
set(PRELOAD_VARIABLE "LD_LIBRARY_PATH")
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_definitions(-DLINUX)
diff --git a/toonz/sources/common/tiio/movsettings.cpp b/toonz/sources/common/tiio/movsettings.cpp
index 6c86e8d3a..af9a7d0eb 100644
--- a/toonz/sources/common/tiio/movsettings.cpp
+++ b/toonz/sources/common/tiio/movsettings.cpp
@@ -1,4 +1,6 @@
-
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0A00
+#endif
#include "texception.h"
#include "tpropertytype.h"
@@ -11,9 +13,9 @@
!(defined(LINUX) || defined(FREEBSD) || defined(HAIKU)) && \
!(defined(__GNUC__) && defined(_WIN32))
-//*******************************************************************************
+//********************************************************************************
// 32-bit version
-//*******************************************************************************
+//********************************************************************************
#include "movsettings.h"
diff --git a/toonz/sources/common/tsystem/tfilepath_io.cpp b/toonz/sources/common/tsystem/tfilepath_io.cpp
index 5f00773f3..1dbd39762 100644
--- a/toonz/sources/common/tsystem/tfilepath_io.cpp
+++ b/toonz/sources/common/tsystem/tfilepath_io.cpp
@@ -1,5 +1,3 @@
-
-
#include "tfilepath_io.h"
#include "tconvert.h"
@@ -10,20 +8,19 @@
#include <fstream>
#include <iostream>
-using namespace std;
#ifdef _MSC_VER
#include <io.h>
#include <windows.h>
-/*!Return a pointer to a \b FILE, if exist. The file is opened using _wfopen_s,
+/*!Returns a pointer to a \b FILE, if it exists. The file is opened using _wfopen_s,
documentation \b
http://msdn2.microsoft.com/en-us/library/z5hh6ee9(VS.80).aspx.
- \b fp is file path, \b mode is the way open, read ("r"), write ("w"),
+ \b fp is the file path, \b mode is the opening mode, read ("r"), write ("w"),
... , to
- know mode view _wfopen_s documentation.
+ understand the mode, refer to _wfopen_s documentation.
*/
-FILE *fopen(const TFilePath &fp, string mode) {
+FILE *fopen(const TFilePath &fp, std::string mode) {
FILE *pFile;
errno_t err =
_wfopen_s(&pFile, fp.getWideString().c_str(), ::to_wstring(mode).c_str());
@@ -31,10 +28,10 @@ FILE *fopen(const TFilePath &fp, string mode) {
return pFile;
}
-Tifstream::Tifstream(const TFilePath &fp) : ifstream(m_file = fopen(fp, "rb")) {
- // manually set the fail bit here, since constructor ifstream::ifstream(FILE*)
- // does not so even if the argument is null pointer.
- // the state will be referred in "TIStream::operator bool()" ( in tstream.cpp )
+Tifstream::Tifstream(const TFilePath &fp) : std::ifstream(m_file = fopen(fp, "rb")) {
+ // Manually set the fail bit here, since the constructor ifstream::ifstream(FILE*)
+ // does not do so, even if the argument is a null pointer.
+ // The state will be checked in "TIStream::operator bool()" (in tstream.cpp)
if (!m_file) setstate(failbit);
}
@@ -51,10 +48,10 @@ void Tifstream::close() {
}
Tofstream::Tofstream(const TFilePath &fp, bool append_existing)
- : ofstream(m_file = fopen(fp, append_existing ? "ab" : "wb")) {
- // manually set the fail bit here, since constructor ofstream::ofstream(FILE*)
- // does not so even if the argument is null pointer.
- // the state will be referred in "TOStream::operator bool()" ( in tstream.cpp )
+ : std::ofstream(m_file = fopen(fp, append_existing ? "ab" : "wb")) {
+ // Manually set the fail bit here, since the constructor ofstream::ofstream(FILE*)
+ // does not do so, even if the argument is a null pointer.
+ // The state will be checked in "TOStream::operator bool()" (in tstream.cpp)
if (!m_file) setstate(failbit);
}
@@ -79,31 +76,30 @@ bool Tofstream::isOpen() const { return m_file != 0; }
//======================
//
-// Versione non windows
+// Non-Windows Version
//
//======================
-FILE *fopen(const TFilePath &fp, string mode) {
+FILE *fopen(const TFilePath &fp, std::string mode) {
return fopen(QString::fromStdWString(fp.getWideString()).toUtf8().data(),
mode.c_str());
}
Tifstream::Tifstream(const TFilePath &fp)
- : ifstream(QString::fromStdWString(fp.getWideString()).toUtf8().data(),
- ios::binary)
+ : std::ifstream(QString::fromStdWString(fp.getWideString()).toUtf8().data(),
+ std::ios::binary)
/*: ifstream(openFileForReading(fp), ios::binary)
-NO! Questo costruttore non e' standard, anche se e' presente
-in molte versioni. Nel MAC non c'e e fa un cast a char*
-sperando che sia il nome del file => compila ma non funziona
+NO! This constructor is non-standard, although it is present in many versions.
+In MAC, it doesn't exist and casts to char* expecting it to be the file name => compiles but doesn't work
*/
{}
Tifstream::~Tifstream() {}
Tofstream::Tofstream(const TFilePath &fp, bool append_existing)
- : ofstream(
+ : std::ofstream(
QString::fromStdWString(fp.getWideString()).toUtf8().data(),
- ios::binary | (append_existing ? ios_base::app : ios_base::trunc)) {}
+ std::ios::binary | (append_existing ? std::ios_base::app : std::ios_base::trunc)) {}
Tofstream::~Tofstream() {}
diff --git a/toonz/sources/common/tsystem/tsystem.cpp b/toonz/sources/common/tsystem/tsystem.cpp
index a1fcdcb62..a21c63792 100644
--- a/toonz/sources/common/tsystem/tsystem.cpp
+++ b/toonz/sources/common/tsystem/tsystem.cpp
@@ -1,13 +1,11 @@
-
-
#include "tsystem.h"
-using namespace std;
-
#include <set>
#include "tfilepath_io.h"
#include "tconvert.h"
+#include <cwctype> // Include the header for towlower
+
#ifndef TNZCORE_LIGHT
#include <QDateTime>
@@ -154,7 +152,7 @@ TFilePath TSystem::getTempDir() {
//------------------------------------------------------------
-TFilePath TSystem::getTestDir(string name) {
+TFilePath TSystem::getTestDir(std::string name) {
return TFilePath("C:") + TFilePath(name);
}
@@ -321,7 +319,7 @@ else
/*
#ifdef _WIN32
-wstring getFormattedMessage(DWORD lastError)
+std::wstring getFormattedMessage(DWORD lastError)
{
LPVOID lpMsgBuf;
FormatMessage(
@@ -338,12 +336,12 @@ FormatMessage(
int wSize = MultiByteToWideChar(0,0,(char*)lpMsgBuf,-1,0,0);
if(!wSize)
- return wstring();
+ return std::wstring();
wchar_t* wBuffer = new wchar_t [wSize+1];
MultiByteToWideChar(0,0,(char*)lpMsgBuf,-1,wBuffer,wSize);
wBuffer[wSize]='\0';
-wstring wmsg(wBuffer);
+std::wstring wmsg(wBuffer);
delete []wBuffer;
LocalFree(lpMsgBuf);
@@ -409,13 +407,12 @@ void TSystem::hideFile(const TFilePath &fp) {
//------------------------------------------------------------
-class CaselessFilepathLess final
- : public std::binary_function<TFilePath, TFilePath, bool> {
+class CaselessFilepathLess final {
public:
bool operator()(const TFilePath &a, const TFilePath &b) const {
// Perform case sensitive compare, fallback to case insensitive.
- const wstring a_str = a.getWideString();
- const wstring b_str = b.getWideString();
+ const std::wstring a_str = a.getWideString();
+ const std::wstring b_str = b.getWideString();
unsigned int i = 0;
int case_compare = -1;
@@ -1045,10 +1042,10 @@ bool TSystem::showDocument(const TFilePath &path) {
}
return true;
#else
- string cmd = "open ";
- string thePath(::to_string(path));
+ std::string cmd = "open ";
+ std::string thePath(::to_string(path));
UINT pos = 0, count = 0;
- // string newPath;
+ // std::string newPath;
char newPath[2048];
while (pos < thePath.size()) {
@@ -1060,12 +1057,25 @@ bool TSystem::showDocument(const TFilePath &path) {
}
newPath[count] = 0;
- cmd = cmd + string(newPath);
+ cmd = cmd + std::string(newPath);
system(cmd.c_str());
return true;
#endif
}
+bool TSystem::isDLLBlackListed(QString dllFile) {
+ QStringList dllBlackList = {"lvcod64.dll", "ff_vfw.dll", "tsccvid64.dll",
+ "hapcodec.dll"};
+
+ for (int x = 0; x < dllBlackList.count(); x++) {
+ if (dllFile.contains(dllBlackList.at(x), Qt::CaseInsensitive)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
#else
#include <windows.h>
@@ -1099,7 +1109,7 @@ TSystemException::TSystemException(const TFilePath &fname,
: m_fname(fname), m_err(-1), m_msg(::to_wstring(msg)) {}
//--------------------------------------------------------------
-TSystemException::TSystemException(const TFilePath &fname, const wstring &msg)
+TSystemException::TSystemException(const TFilePath &fname, const std::wstring &msg)
: m_fname(fname), m_err(-1), m_msg(msg) {}
//--------------------------------------------------------------
@@ -1108,5 +1118,5 @@ TSystemException::TSystemException(const std::string &msg)
: m_fname(""), m_err(-1), m_msg(::to_wstring(msg)) {}
//--------------------------------------------------------------
-TSystemException::TSystemException(const wstring &msg)
+TSystemException::TSystemException(const std::wstring &msg)
: m_fname(""), m_err(-1), m_msg(msg) {}
diff --git a/toonz/sources/common/tvrender/tofflinegl.cpp b/toonz/sources/common/tvrender/tofflinegl.cpp
index 0908fe82a..fc8f00f6b 100644
--- a/toonz/sources/common/tvrender/tofflinegl.cpp
+++ b/toonz/sources/common/tvrender/tofflinegl.cpp
@@ -855,8 +855,7 @@ int TOfflineGL::getLy() const { return m_imp->getLy(); }
namespace {
-struct DimensionLess final
- : public std::binary_function<TDimension, TDimension, bool> {
+struct DimensionLess final {
bool operator()(const TDimension &d1, const TDimension &d2) const {
return d1.lx < d2.lx || (d1.lx == d2.lx && d1.ly < d2.ly);
}
diff --git a/toonz/sources/common/tvrender/tsimplecolorstyles.cpp b/toonz/sources/common/tvrender/tsimplecolorstyles.cpp
index 440ee0d49..1b850c94c 100644
--- a/toonz/sources/common/tvrender/tsimplecolorstyles.cpp
+++ b/toonz/sources/common/tvrender/tsimplecolorstyles.cpp
@@ -385,14 +385,14 @@ public:
};
//----------------------------------------------
- struct KeyLess final : public std::binary_function<Key, Key, bool> {
+ struct KeyLess final {
bool operator()(const Key &d1, const Key &d2) const {
- return d1.m_glContext < d2.m_glContext ||
- (d1.m_glContext == d2.m_glContext &&
- (d1.m_fid < d2.m_fid ||
- d1.m_fid == d2.m_fid && d1.m_name < d2.m_name));
+ return d1.m_glContext < d2.m_glContext ||
+ (d1.m_glContext == d2.m_glContext &&
+ (d1.m_fid < d2.m_fid ||
+ d1.m_fid == d2.m_fid && d1.m_name < d2.m_name));
}
- };
+};
//----------------------------------------------
diff --git a/toonz/sources/include/tpropertytype.h b/toonz/sources/include/tpropertytype.h
index dd3db9b25..c346e763d 100644
--- a/toonz/sources/include/tpropertytype.h
+++ b/toonz/sources/include/tpropertytype.h
@@ -4,7 +4,6 @@
#define TPROPERTYTYPE_H
#include <vector>
-using namespace std;
template <typename T>
class TEnumerationT {
@@ -13,12 +12,12 @@ public:
TEnumerationT() {}
- void addItem(const string &id, T item) {
+ void addItem(const std::string &id, T item) {
m_items.push_back(std::make_pair(id, item));
}
int getItemCount() const { return m_items.size(); }
- void getItem(unsigned int i, string &idstring, T &value) const {
+ void getItem(unsigned int i, std::string &idstring, T &value) const {
assert(i < m_items.size());
ItemData vp = m_items[i];
idstring = vp.first;
@@ -32,14 +31,14 @@ public:
}
bool isValid(T item) const {
- typename vector<ItemData>::const_iterator it =
+ typename std::vector<ItemData>::const_iterator it =
find_if(m_items.begin(), m_items.end(), MatchesItem(item));
return it != m_items.end();
}
private:
- typedef pair<string, T> ItemData;
- vector<ItemData> m_items;
+ typedef std::pair<std::string, T> ItemData;
+ std::vector<ItemData> m_items;
class MatchesItem {
public:
diff --git a/toonz/sources/mousedragfilter/CMakeLists.txt b/toonz/sources/mousedragfilter/CMakeLists.txt
index 5ca8767af..d825b6c12 100644
--- a/toonz/sources/mousedragfilter/CMakeLists.txt
+++ b/toonz/sources/mousedragfilter/CMakeLists.txt
@@ -1,5 +1,5 @@
if(BUILD_ENV_APPLE)
- cmake_minimum_required(VERSION 3.10)
+ cmake_minimum_required(VERSION 2.8.11)
project(MouseDragFilter)
include_directories(.)
diff --git a/toonz/sources/stdfx/shaderfx.cpp b/toonz/sources/stdfx/shaderfx.cpp
index 5954883d3..22cdcd83c 100644
--- a/toonz/sources/stdfx/shaderfx.cpp
+++ b/toonz/sources/stdfx/shaderfx.cpp
@@ -883,9 +883,9 @@ void ShaderFx::getInputData(const TRectD &rect, double frame,
#else
std::vector<const GLchar *> varyingNames(
boost::make_transform_iterator(varyingStrings.begin(),
- std::mem_fun_ref(&std::string::c_str)),
+ std::mem_fn(&std::string::c_str)),
boost::make_transform_iterator(varyingStrings.end(),
- std::mem_fun_ref(&std::string::c_str)));
+ std::mem_fn(&std::string::c_str)));
#endif
prog = touchShaderProgram(sd, context, int(varyingNames.size()),
&varyingNames[0]);
@@ -1203,4 +1203,4 @@ void loadShaderInterfaces(const TFilePath &shadersFolder) {
new ShaderFxDeclaration(shaderInterface)));
}
}
-}
+}
\ No newline at end of file
diff --git a/toonz/sources/tnzext/NotSymmetricBezierPotential.cpp b/toonz/sources/tnzext/NotSymmetricBezierPotential.cpp
index 6ab4a7798..9338c82ba 100644
--- a/toonz/sources/tnzext/NotSymmetricBezierPotential.cpp
+++ b/toonz/sources/tnzext/NotSymmetricBezierPotential.cpp
@@ -5,187 +5,180 @@
#include <tcurves.h>
#include <algorithm>
-#include <functional>
-
-using namespace std;
-
-using namespace std;
-using namespace ToonzExt;
-//-----------------------------------------------------------------------------
+#include <cassert>
+#include <cmath>
namespace {
-typedef unary_function<double, double> unary_functionDD;
//---------------------------------------------------------------------------
-class myBlendFunc : unary_functionDD {
- // TCubic c;
- TQuadratic curve;
+struct myBlendFunc final {
+ TQuadratic curve;
public:
- myBlendFunc(double t = 0.0) {
- curve.setP0(TPointD(0.0, 1.0));
- curve.setP1(TPointD(0.5 * (1.0 - t), 1.0));
- curve.setP2(TPointD(1.0, 0.0));
- }
-
- result_type operator()(argument_type x) {
- result_type out = 0.0;
- x = fabs(x);
- if (x >= 1.0) return 0.0;
- out = curve.getPoint(x).y;
- return out;
- }
+ myBlendFunc(double t = 0.0) {
+ // Initialize the quadratic curve with control points
+ curve.setP0(TPointD(0.0, 1.0)); // Start point
+ curve.setP1(TPointD(0.5 * (1.0 - t), 1.0)); // Control point
+ curve.setP2(TPointD(1.0, 0.0)); // End point
+ }
+
+ double operator()(double x) {
+ double out = 0.0;
+ x = std::fabs(x); // Use absolute value of x
+ if (x >= 1.0) return 0.0; // Return 0 if x is outside the range
+ out = curve.getPoint(x).y; // Get the y-coordinate of the curve at x
+ return out;
+ }
};
-}
-//-----------------------------------------------------------------------------
+} // namespace
+
+namespace ToonzExt {
-ToonzExt::NotSymmetricBezierPotential::~NotSymmetricBezierPotential() {}
+NotSymmetricBezierPotential::~NotSymmetricBezierPotential() {}
//-----------------------------------------------------------------------------
-void ToonzExt::NotSymmetricBezierPotential::setParameters_(const TStroke *ref,
- double w,
- double al) {
- assert(ref);
- ref_ = ref;
- par_ = w;
- actionLength_ = al;
+void NotSymmetricBezierPotential::setParameters_(const TStroke* ref, double w, double al) {
+ assert(ref); // Ensure the reference stroke is valid
+ ref_ = ref;
+ par_ = w; // Parameter along the stroke
+ actionLength_ = al; // Length of the action
- strokeLength_ = ref->getLength();
- lengthAtParam_ = ref->getLength(par_);
+ strokeLength_ = ref->getLength(); // Total length of the stroke
+ lengthAtParam_ = ref->getLength(par_); // Length at the parameter point
- // lunghezza dal pto di click all'inizio della curva
- leftFactor_ = min(lengthAtParam_,
- actionLength_ * 0.5); // lengthAtParam_ / strokeLength_;
+ // Length from the click point to the start of the curve
+ leftFactor_ = std::min(lengthAtParam_, actionLength_ * 0.5);
- // lunghezza dal pto di click alla fine
- rightFactor_ = min(strokeLength_ - lengthAtParam_, actionLength_ * 0.5);
+ // Length from the click point to the end of the curve
+ if (areAlmostEqual(strokeLength_, lengthAtParam_, 0.001))
+ rightFactor_ = 0.0; // If the click point is at the end, set rightFactor to 0
+ else
+ rightFactor_ = std::min(strokeLength_ - lengthAtParam_, actionLength_ * 0.5);
}
//-----------------------------------------------------------------------------
-double ToonzExt::NotSymmetricBezierPotential::value_(double value2test) const {
- assert(0.0 <= value2test && value2test <= 1.0);
- return this->compute_value(value2test);
+double NotSymmetricBezierPotential::value_(double value2test) const {
+ assert(0.0 <= value2test && value2test <= 1.0); // Ensure the input is within valid range
+ return this->compute_value(value2test); // Compute the value at the given parameter
}
//-----------------------------------------------------------------------------
-// normalization of parameter in range interval
-double ToonzExt::NotSymmetricBezierPotential::compute_shape(
- double value2test) const {
- double x = ref_->getLength(value2test);
- double shape = this->actionLength_ * 0.5;
- if (isAlmostZero(shape)) shape = 1.0;
- x = (x - lengthAtParam_) / shape;
- return x;
+// Normalization of the parameter within the range interval
+double NotSymmetricBezierPotential::compute_shape(double value2test) const {
+ double x = ref_->getLength(value2test); // Get the length at the parameter
+ double shape = this->actionLength_ * 0.5; // Half of the action length
+ if (isAlmostZero(shape)) shape = 1.0; // Avoid division by zero
+ x = (x - lengthAtParam_) / shape; // Normalize the parameter
+ return x;
}
//-----------------------------------------------------------------------------
-double ToonzExt::NotSymmetricBezierPotential::compute_value(
- double value2test) const {
- myBlendFunc me;
+double NotSymmetricBezierPotential::compute_value(double value2test) const {
+ myBlendFunc me; // Create a blending function
- // on extremes use
- // 2
- // 1-x
- //
+ // On extremes, use a quadratic falloff: 1 - x^2
+ // 2
+ // 1-x
+ //
- // when is near to extreme uses a mix notation
- double x = 0.0;
- double res = 0.0;
+ double x = 0.0;
+ double res = 0.0;
- // length at parameter
- x = ref_->getLength(value2test);
+ // Length at the parameter
+ x = ref_->getLength(value2test);
- const double tolerance = 0.0; // need to be pixel based
+ const double tolerance = 0.0; // Tolerance for comparison (pixel-based)
- // if is an extreme
- if (max(lengthAtParam_, 0.0) < tolerance ||
- max(strokeLength_ - lengthAtParam_, 0.0) < tolerance) {
- double tmp_al = actionLength_ * 0.5;
+ // If the parameter is at an extreme (start or end of the stroke)
+ if (std::max(lengthAtParam_, 0.0) < tolerance ||
+ std::max(strokeLength_ - lengthAtParam_, 0.0) < tolerance) {
+ double tmp_al = actionLength_ * 0.5;
- // compute correct parameter considering offset
- // try to have a square curve like shape
- //
- // 2
- // m = x
- //
- if (leftFactor_ <= tolerance)
- x = 1.0 - x / tmp_al;
- else
- x = (x - (strokeLength_ - tmp_al)) / tmp_al;
-
- if (x < 0.0) return 0.0;
- assert(0.0 <= x && x <= 1.0 + TConsts::epsilon);
- x = std::min(x, 1.0); // just to avoid problem in production code
- res = sq(x);
- } else // when is not an extreme
- {
- double length_at_value2test = ref_->getLength(value2test);
-
- const double min_level = 0.01;
- // if check a parameter over click point
- if (length_at_value2test >= lengthAtParam_) {
- // check if extreme can be moved from this parameter configuration
- double tmp_x = this->compute_shape(1.0);
- double tmp_res = me(tmp_x);
- if (tmp_res > min_level) {
- // please note that in this case
- // lengthAtParam_ + rightFactor_ == strokeLength_
- // (by ctor).
- if (rightFactor_ != 0.0)
- x = (length_at_value2test - lengthAtParam_) / rightFactor_;
+ // Compute the correct parameter considering the offset
+ // Use a quadratic falloff: 1 - x^2
+ //
+ // 2
+ // m = x
+ //
+ if (leftFactor_ <= tolerance)
+ x = 1.0 - x / tmp_al;
else
- x = 0.0;
+ x = (x - (strokeLength_ - tmp_al)) / tmp_al;
- assert(0.0 - TConsts::epsilon <= x && x <= 1.0 + TConsts::epsilon);
- if (isAlmostZero(x)) x = 0.0;
- if (areAlmostEqual(x, 1.0)) x = 1.0;
-
- double how_many_of_shape =
- (strokeLength_ - lengthAtParam_) / (actionLength_ * 0.5);
- assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0);
-
- myBlendFunc bf(how_many_of_shape);
-
- return bf(x);
- }
+ if (x < 0.0) return 0.0; // Ensure x is within bounds
+ assert(0.0 <= x && x <= 1.0 + TConsts::epsilon); // Debug check
+ x = std::min(x, 1.0); // Clamp x to 1.0
+ res = x * x; // Quadratic falloff
} else {
- // leftFactor_
- double tmp_x = this->compute_shape(0.0);
- double tmp_res = me(tmp_x);
- if (tmp_res > min_level) {
- double x = length_at_value2test / leftFactor_;
- assert(0.0 <= x && x <= 1.0);
-
- // then movement use another shape
- double diff = x - 1.0;
-
- double how_many_of_shape = lengthAtParam_ / (actionLength_ * 0.5);
- assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0);
-
- myBlendFunc bf(how_many_of_shape);
- return bf(diff);
- }
+ // When the parameter is not at an extreme
+ double length_at_value2test = ref_->getLength(value2test);
+ const double min_level = 0.01; // Minimum level for blending
+
+ // If the parameter is after the click point
+ if (length_at_value2test >= lengthAtParam_) {
+ // Check if the extreme can be moved from this parameter configuration
+ double tmp_x = this->compute_shape(1.0);
+ double tmp_res = me(tmp_x);
+ if (tmp_res > min_level) {
+ // Compute the normalized parameter
+ if (rightFactor_ != 0.0)
+ x = (length_at_value2test - lengthAtParam_) / rightFactor_;
+ else
+ x = 0.0;
+
+ assert(0.0 - TConsts::epsilon <= x && x <= 1.0 + TConsts::epsilon); // Debug check
+ if (isAlmostZero(x)) x = 0.0; // Handle edge cases
+ if (areAlmostEqual(x, 1.0)) x = 1.0;
+
+ // Compute the blending factor
+ double how_many_of_shape = (strokeLength_ - lengthAtParam_) / (actionLength_ * 0.5);
+ assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0); // Debug check
+
+ // Apply the blending function
+ myBlendFunc bf(how_many_of_shape);
+ return bf(x);
+ }
+ } else {
+ // If the parameter is before the click point
+ double tmp_x = this->compute_shape(0.0);
+ double tmp_res = me(tmp_x);
+ if (tmp_res > min_level) {
+ double x = length_at_value2test / leftFactor_;
+ assert(0.0 <= x && x <= 1.0); // Debug check
+
+ // Compute the difference
+ double diff = x - 1.0;
+
+ // Compute the blending factor
+ double how_many_of_shape = lengthAtParam_ / (actionLength_ * 0.5);
+ assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0); // Debug check
+
+ // Apply the blending function
+ myBlendFunc bf(how_many_of_shape);
+ return bf(diff);
+ }
+ }
+
+ // Default behavior is an exponential falloff
+ x = this->compute_shape(value2test);
+ res = me(x);
}
-
- // default behaviour is an exp
- x = this->compute_shape(value2test);
- res = me(x);
- }
- return res;
+ return res;
}
//-----------------------------------------------------------------------------
-ToonzExt::Potential *ToonzExt::NotSymmetricBezierPotential::clone() {
- return new NotSymmetricBezierPotential;
+Potential* NotSymmetricBezierPotential::clone() {
+ return new NotSymmetricBezierPotential; // Return a new instance of the potential
}
+} // namespace ToonzExt
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
diff --git a/toonz/sources/tnzext/NotSymmetricExpPotential.cpp b/toonz/sources/tnzext/NotSymmetricExpPotential.cpp
index 1ed2245f0..802d6179c 100644
--- a/toonz/sources/tnzext/NotSymmetricExpPotential.cpp
+++ b/toonz/sources/tnzext/NotSymmetricExpPotential.cpp
@@ -1,85 +1,83 @@
-
-
#include "ext/NotSymmetricExpPotential.h"
#include <tmathutil.h>
#include <algorithm>
-#include <functional>
-
-using namespace std;
+#include <cassert>
+#include <cmath>
//-----------------------------------------------------------------------------
namespace {
-typedef unary_function<double, double> unary_functionDD;
//---------------------------------------------------------------------------
-class mySqr : unary_functionDD {
+struct mySqr final {
public:
- result_type operator()(argument_type x) { return 1.0 - sq(x); }
+ double operator()(double x) { return 1.0 - sq(x); } // Compute 1 - x^2
};
//---------------------------------------------------------------------------
-class myExp : unary_functionDD {
+struct myExp final {
public:
- result_type operator()(argument_type x) { return exp(-sq(x)); }
+ double operator()(double x) { return exp(-sq(x)); } // Compute exp(-x^2)
};
//---------------------------------------------------------------------------
-struct blender {
- double operator()(double a, double b, double t) {
- assert(0.0 <= t && t <= 1.0);
- return (a * (1.0 - t) + b * t);
- }
+struct blender {
+ double operator()(double a, double b, double t) {
+ assert(0.0 <= t && t <= 1.0); // Ensure t is in the range [0, 1]
+ return (a * (1.0 - t) + b * t); // Linear interpolation between a and b
+ }
};
//---------------------------------------------------------------------------
struct blender_2 {
- double operator()(double a, double b, double t) {
- assert(0.0 <= t && t <= 1.0);
-
- // a(1-t)^2 + 2t*(1-t) + t^2
- double one_t = 1.0 - t;
- double num = 3.0;
- double den = 4.0;
- // solution of a(1-t)^2 + P*2t(1-t) + b*t^2 = (a+b)/2
- // with t = num/den
- double middle =
- sq(den) / (2.0 * num) * ((a + b) * 0.5 - (a + sq(num) * b) / sq(den));
-
- return a * sq(one_t) + middle * t * one_t + b * sq(t);
- }
+ double operator()(double a, double b, double t) {
+ assert(0.0 <= t && t <= 1.0); // Ensure t is in the range [0, 1]
+
+ // Quadratic interpolation: a(1-t)^2 + 2t*(1-t) + t^2
+ double one_t = 1.0 - t;
+ double num = 3.0;
+ double den = 4.0;
+
+ // Solve for the middle point to ensure the average of a and b at t = num/den
+ double middle =
+ sq(den) / (2.0 * num) * ((a + b) * 0.5 - (a + sq(num) * b) / sq(den));
+
+ return a * sq(one_t) + middle * t * one_t + b * sq(t); // Quadratic blend
+ }
};
-}
+
+} // namespace
//-----------------------------------------------------------------------------
-void ToonzExt::NotSymmetricExpPotential::setParameters_(const TStroke *ref,
- double par, double al) {
- ref_ = ref;
- par_ = par;
- actionLength_ = al;
+void ToonzExt::NotSymmetricExpPotential::setParameters_(const TStroke* ref, double par, double al) {
+ ref_ = ref;
+ par_ = par; // Parameter along the stroke
+ actionLength_ = al; // Length of the action
- assert(ref_);
+ assert(ref_); // Ensure the reference stroke is valid
- strokeLength_ = ref->getLength();
- lengthAtParam_ = ref->getLength(par);
+ strokeLength_ = ref->getLength(); // Total length of the stroke
+ lengthAtParam_ = ref->getLength(par); // Length at the parameter point
- // lunghezza dal pto di click all'inizio della curva
- leftFactor_ = min(lengthAtParam_,
- actionLength_ * 0.5); // lengthAtParam_ / strokeLength_;
+ // Length from the click point to the start of the curve
+ leftFactor_ = std::min(lengthAtParam_, actionLength_ * 0.5);
- // lunghezza dal pto di click alla fine
- rightFactor_ = min(strokeLength_ - lengthAtParam_, actionLength_ * 0.5);
+ // Length from the click point to the end of the curve
+ if (areAlmostEqual(strokeLength_, lengthAtParam_, 0.001))
+ rightFactor_ = 0.0; // If the click point is at the end, set rightFactor to 0
+ else
+ rightFactor_ = std::min(strokeLength_ - lengthAtParam_, actionLength_ * 0.5);
- // considero come intervallo di mapping [-range,range].
- // 4 ha come valore c.a. 10exp-6
- // cioé sposterei un pixel su un movimento di un milione di pixel
- range_ = 2.8;
+ // Consider the mapping interval as [-range_, range_].
+ // A value of 2.8 corresponds to approximately 10^-6,
+ // meaning a movement of 1 pixel in a million pixels.
+ range_ = 2.8;
}
//-----------------------------------------------------------------------------
@@ -89,129 +87,118 @@ ToonzExt::NotSymmetricExpPotential::~NotSymmetricExpPotential() {}
//-----------------------------------------------------------------------------
double ToonzExt::NotSymmetricExpPotential::value_(double value2test) const {
- assert(0.0 <= value2test && value2test <= 1.0);
- return this->compute_value(value2test);
+ assert(0.0 <= value2test && value2test <= 1.0); // Ensure the input is within valid range
+ return this->compute_value(value2test); // Compute the value at the given parameter
}
//-----------------------------------------------------------------------------
-// normalization of parameter in range interval
-double ToonzExt::NotSymmetricExpPotential::compute_shape(
- double value2test) const {
- double x = ref_->getLength(value2test);
- double shape = this->actionLength_ * 0.5;
- if (isAlmostZero(shape)) shape = 1.0;
- x = ((x - lengthAtParam_) * range_) / shape;
- return x;
+// Normalization of the parameter within the range interval
+double ToonzExt::NotSymmetricExpPotential::compute_shape(double value2test) const {
+ double x = ref_->getLength(value2test); // Get the length at the parameter
+ double shape = this->actionLength_ * 0.5; // Half of the action length
+ if (isAlmostZero(shape)) shape = 1.0; // Avoid division by zero
+ x = ((x - lengthAtParam_) * range_) / shape; // Normalize the parameter
+ return x;
}
//-----------------------------------------------------------------------------
-double ToonzExt::NotSymmetricExpPotential::compute_value(
- double value2test) const {
- myExp me;
- mySqr ms;
- blender op;
-
- // usually use the form below
- // ( ) 2
- // ( (x - l)*range_ )
- // - (--------------- )
- // ( factor )
- // l,r
- // e
- //
- // where factor is computed like in constructor
-
- // on extremes use
- // 2
- // 1-x
- //
-
- // when is near to extreme uses a mix notation
- double x = 0.0;
- double res = 0.0;
-
- // length at parameter
- x = ref_->getLength(value2test);
-
- const double tolerance = 2.0; // need to be pixel based
- // if is an extreme
- if (max(lengthAtParam_, 0.0) < tolerance ||
- max(strokeLength_ - lengthAtParam_, 0.0) < tolerance) {
- double tmp_al = actionLength_ * 0.5;
-
- // compute correct parameter considering offset
- // try to have a square curve like shape
- //
- // 2
- // m = x
- //
- if (leftFactor_ <= tolerance)
- x = 1.0 - x / tmp_al;
- else
- x = (x - (strokeLength_ - tmp_al)) / tmp_al;
-
- if (x < 0.0) return 0.0;
- assert(0.0 <= x && x <= 1.0 + TConsts::epsilon);
- res = sq(x);
- } else // when is not an extreme
- {
- double length_at_value2test = ref_->getLength(value2test);
-
- const double min_level = 0.01;
- // if check a parameter over click point
- if (length_at_value2test >= lengthAtParam_) {
- // check if extreme can be moved from this parameter configuration
- double tmp_x = this->compute_shape(1.0);
- double tmp_res = me(tmp_x);
- if (tmp_res > min_level) {
- // please note that in this case
- // lengthAtParam_ + rightFactor_ == strokeLength_
- // (by ctor).
- x = (length_at_value2test - lengthAtParam_) / rightFactor_;
- assert(0.0 <= x && x <= 1.0);
-
- // then movement use another shape
- double exp_val = me(x * range_);
-
- double how_many_of_shape =
- (strokeLength_ - lengthAtParam_) / (actionLength_ * 0.5);
- assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0);
-
- // return ms(x);
- return op(ms(x), exp_val, how_many_of_shape);
- }
+double ToonzExt::NotSymmetricExpPotential::compute_value(double value2test) const {
+ myExp me; // Exponential function
+ mySqr ms; // Quadratic function
+ blender op; // Linear blending function
+
+ // On extremes, use a quadratic falloff: 1 - x^2
+ // ( ) 2
+ // ( (x - l)*range_ )
+ // - (--------------- )
+ // ( factor )
+ // l,r
+ // When near an extreme, use a mix of quadratic and exponential functions
+
+ double x = 0.0;
+ double res = 0.0;
+
+ // Length at the parameter
+ x = ref_->getLength(value2test);
+
+ const double tolerance = 2.0; // Tolerance for comparison (pixel-based)
+
+ // If the parameter is at an extreme (start or end of the stroke)
+ if (std::max(lengthAtParam_, 0.0) < tolerance ||
+ std::max(strokeLength_ - lengthAtParam_, 0.0) < tolerance) {
+ double tmp_al = actionLength_ * 0.5;
+
+ // Compute the correct parameter considering the offset
+ // Use a quadratic falloff: 1 - x^2
+ if (leftFactor_ <= tolerance)
+ x = 1.0 - x / tmp_al;
+ else
+ x = (x - (strokeLength_ - tmp_al)) / tmp_al;
+
+ if (x < 0.0) return 0.0; // Ensure x is within bounds
+ assert(0.0 <= x && x <= 1.0 + TConsts::epsilon); // Debug check
+ res = sq(x); // Quadratic falloff
} else {
- // leftFactor_
- double tmp_x = this->compute_shape(0.0);
- double tmp_res = me(tmp_x);
- if (tmp_res > min_level) {
- double x = length_at_value2test / leftFactor_;
- assert(0.0 <= x && x <= 1.0);
-
- // then movement use another shape
- double diff = x - 1.0;
- double exp_val = me(diff * range_);
- double how_many_of_shape = lengthAtParam_ / (actionLength_ * 0.5);
- assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0);
-
- // return ms(diff);
- return op(ms(diff), exp_val, how_many_of_shape);
- }
+ // When the parameter is not at an extreme
+ double length_at_value2test = ref_->getLength(value2test);
+ const double min_level = 0.01; // Minimum level for blending
+
+ // If the parameter is after the click point
+ if (length_at_value2test >= lengthAtParam_) {
+ // Check if the extreme can be moved from this parameter configuration
+ double tmp_x = this->compute_shape(1.0);
+ double tmp_res = me(tmp_x);
+ if (tmp_res > min_level) {
+ // Compute the normalized parameter
+ x = (length_at_value2test - lengthAtParam_) / rightFactor_;
+ assert(0.0 <= x && x <= 1.0); // Debug check
+
+ // Apply the exponential function
+ double exp_val = me(x * range_);
+
+ // Compute the blending factor
+ double how_many_of_shape = (strokeLength_ - lengthAtParam_) / (actionLength_ * 0.5);
+ assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0); // Debug check
+
+ // Blend the quadratic and exponential functions
+ return op(ms(x), exp_val, how_many_of_shape);
+ }
+ } else {
+ // If the parameter is before the click point
+ double tmp_x = this->compute_shape(0.0);
+ double tmp_res = me(tmp_x);
+ if (tmp_res > min_level) {
+ double x = length_at_value2test / leftFactor_;
+ assert(0.0 <= x && x <= 1.0); // Debug check
+
+ // Compute the difference
+ double diff = x - 1.0;
+
+ // Apply the exponential function
+ double exp_val = me(diff * range_);
+
+ // Compute the blending factor
+ double how_many_of_shape = lengthAtParam_ / (actionLength_ * 0.5);
+ assert(0.0 <= how_many_of_shape && how_many_of_shape <= 1.0); // Debug check
+
+ // Blend the quadratic and exponential functions
+ return op(ms(diff), exp_val, how_many_of_shape);
+ }
+ }
+
+ // Default behavior is an exponential falloff
+ x = this->compute_shape(value2test);
+ res = me(x);
}
-
- // default behaviour is an exp
- x = this->compute_shape(value2test);
- res = me(x);
- }
- return res;
+ return res;
}
//-----------------------------------------------------------------------------
-ToonzExt::Potential *ToonzExt::NotSymmetricExpPotential::clone() {
- return new NotSymmetricExpPotential;
+ToonzExt::Potential* ToonzExt::NotSymmetricExpPotential::clone() {
+ return new NotSymmetricExpPotential; // Return a new instance of the potential
}
// DEL double ToonzExt::NotSymmetricExpPotential::gradient(double value2test)