Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct float literals, 32/64 trunc, unref vars #268

Merged
merged 1 commit into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/ImageManip/image_manip_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int main() {
imageManip->initialConfig.setResizeThumbnail(300, 400);

// Second image manipulator - Create a off center crop
imageManip2->initialConfig.setCropRect(0.1, 0.1, 0.3, 0.3);
imageManip2->initialConfig.setCropRect(0.1f, 0.1f, 0.3f, 0.3f);
imageManip2->setWaitForConfigInput(true);

// Linking
Expand Down
10 changes: 5 additions & 5 deletions examples/ImageManip/rgb_rotate_warp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void printControls() {
printf(" h -print controls (help)\n");
}

static constexpr auto ROTATE_RATE_MAX = 5.0;
static constexpr auto ROTATE_RATE_INC = 0.1;
static constexpr auto ROTATE_RATE_MAX = 5.0f;
static constexpr auto ROTATE_RATE_INC = 0.1f;

static constexpr auto RESIZE_MAX_W = 800;
static constexpr auto RESIZE_MAX_H = 600;
Expand Down Expand Up @@ -49,9 +49,9 @@ std::vector<warpFourPointTest> warpList = {
{{P1, P2, P3, P0}, true, "4. rotate 270"},
{{P1, P0, P3, P2}, true, "5. horizontal mirror"},
{{P3, P2, P1, P0}, true, "6. vertical flip"},
{{{-0.1, -0.1}, {1.1, -0.1}, {1.1, 1.1}, {-0.1, 1.1}}, true, "7. add black borders"},
{{{-0.3, 0}, {1, 0}, {1.3, 1}, {0, 1}}, true, "8. parallelogram transform"},
{{{-0.2, 0}, {1.8, 0}, {1, 1}, {0, 1}}, true, "9. trapezoid transform"},
{{{-0.1f, -0.1f}, {1.1f, -0.1f}, {1.1f, 1.1f}, {-0.1f, 1.1f}}, true, "7. add black borders"},
{{{-0.3f, 0}, {1, 0}, {1.3f, 1}, {0, 1}}, true, "8. parallelogram transform"},
{{{-0.2f, 0}, {1.8f, 0}, {1, 1}, {0, 1}}, true, "9. trapezoid transform"},
};

int main() {
Expand Down
6 changes: 3 additions & 3 deletions examples/MonoCamera/mono_camera_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "depthai/depthai.hpp"

// Step size ('W','A','S','D' controls)
static constexpr float stepSize = 0.02;
static constexpr float stepSize = 0.02f;

// Manual exposure/focus set step
static constexpr int EXP_STEP = 500; // us
Expand Down Expand Up @@ -46,8 +46,8 @@ int main() {
manipOutLeft->setStreamName("left");

// Crop range
dai::Point2f topLeft(0.2, 0.2);
dai::Point2f bottomRight(0.8, 0.8);
dai::Point2f topLeft(0.2f, 0.2f);
dai::Point2f bottomRight(0.8f, 0.8f);

// Properties
monoRight->setBoardSocket(dai::CameraBoardSocket::RIGHT);
Expand Down
2 changes: 1 addition & 1 deletion examples/SpatialDetection/spatial_location_calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Inludes common necessary includes for development using depthai library
#include "depthai/depthai.hpp"

static constexpr float stepSize = 0.05;
static constexpr float stepSize = 0.05f;

static std::atomic<bool> newConfig{false};

Expand Down
6 changes: 3 additions & 3 deletions examples/StereoDepth/depth_crop_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "depthai/depthai.hpp"

// Step size ('W','A','S','D' controls)
static constexpr float stepSize = 0.02;
static constexpr float stepSize = 0.02f;

int main() {
// Create pipeline
Expand All @@ -27,8 +27,8 @@ int main() {
xout->setStreamName("depth");

// Crop range
dai::Point2f topLeft(0.2, 0.2);
dai::Point2f bottomRight(0.8, 0.8);
dai::Point2f topLeft(0.2f, 0.2f);
dai::Point2f bottomRight(0.8f, 0.8f);

// Properties
monoRight->setBoardSocket(dai::CameraBoardSocket::RIGHT);
Expand Down
12 changes: 7 additions & 5 deletions src/device/CalibrationHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ CalibrationHandler::CalibrationHandler(std::string calibrationDataPath, std::str
}

file.seekg(0, file.end);
unsigned fSize = file.tellg();
const unsigned fSize = static_cast<unsigned>(file.tellg());
file.seekg(0, file.beg);

if(fSize != versionSize) {
Expand Down Expand Up @@ -205,10 +205,12 @@ std::vector<std::vector<float>> CalibrationHandler::getCameraIntrinsics(
std::vector<std::vector<float>> intrinsicMatrix = eepromData.cameraData[cameraId].intrinsicMatrix;
if(resizeWidth != -1 || resizeHeight != -1) {
if(resizeWidth == -1) {
resizeWidth = eepromData.cameraData[cameraId].width * resizeHeight / static_cast<float>(eepromData.cameraData[cameraId].height);
resizeWidth = static_cast<decltype(resizeWidth)>(eepromData.cameraData[cameraId].width * resizeHeight
/ static_cast<float>(eepromData.cameraData[cameraId].height));
}
if(resizeHeight == -1) {
resizeHeight = eepromData.cameraData[cameraId].height * resizeWidth / static_cast<float>(eepromData.cameraData[cameraId].width);
resizeHeight = static_cast<decltype(resizeHeight)>(eepromData.cameraData[cameraId].height * resizeWidth
/ static_cast<float>(eepromData.cameraData[cameraId].width));
}
float scale = resizeHeight / static_cast<float>(eepromData.cameraData[cameraId].height);
if(scale * eepromData.cameraData[cameraId].width < resizeWidth) {
Expand Down Expand Up @@ -246,7 +248,7 @@ std::vector<std::vector<float>> CalibrationHandler::getCameraIntrinsics(CameraBo
Size2f destShape,
Point2f topLeftPixelId,
Point2f bottomRightPixelId) {
return getCameraIntrinsics(cameraId, destShape.width, destShape.height, topLeftPixelId, bottomRightPixelId);
return getCameraIntrinsics(cameraId, static_cast<int>(destShape.width), static_cast<int>(destShape.height), topLeftPixelId, bottomRightPixelId);
}

std::vector<std::vector<float>> CalibrationHandler::getCameraIntrinsics(CameraBoardSocket cameraId,
Expand Down Expand Up @@ -513,7 +515,7 @@ void CalibrationHandler::setCameraIntrinsics(CameraBoardSocket cameraId, std::ve
}

void CalibrationHandler::setCameraIntrinsics(CameraBoardSocket cameraId, std::vector<std::vector<float>> intrinsics, Size2f frameSize) {
setCameraIntrinsics(cameraId, intrinsics, frameSize.width, frameSize.height);
setCameraIntrinsics(cameraId, intrinsics, static_cast<int>(frameSize.width), static_cast<int>(frameSize.height));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/device/DeviceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ void DeviceBase::tryStartPipeline(const Pipeline& pipeline) {
if(!startPipeline(pipeline)) {
throw std::runtime_error("Couldn't start the pipeline");
}
} catch(const std::exception& e) {
} catch(const std::exception&) {
// close device (cleanup)
close();
// Rethrow original exception
Expand Down Expand Up @@ -467,7 +467,7 @@ void DeviceBase::init2(Config cfg, const std::string& pathToMvcmd, tl::optional<
// Try parsing the string as a number
try {
std::chrono::milliseconds watchdog{std::stoi(watchdogMsStr)};
config.preboot.watchdogTimeoutMs = watchdog.count();
config.preboot.watchdogTimeoutMs = static_cast<uint32_t>(watchdog.count());
watchdogTimeout = watchdog;
spdlog::debug("Using a custom watchdog value of {}", watchdogTimeout);
} catch(const std::invalid_argument& e) {
Expand Down
21 changes: 13 additions & 8 deletions src/device/DeviceBootloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,16 @@ std::vector<uint8_t> DeviceBootloader::createDepthaiApplicationPackage(const Pip
using namespace std::chrono;

auto t1 = steady_clock::now();
auto compressBufferSize = compressBound(deviceFirmware.size());
auto compressBufferSize = compressBound(static_cast<decltype(compressBound(1))>(deviceFirmware.size()));
std::vector<uint8_t> compressBuffer(compressBufferSize);
// Chosen impirically
constexpr int COMPRESSION_LEVEL = 9;
if(compress2(compressBuffer.data(), &compressBufferSize, deviceFirmware.data(), deviceFirmware.size(), COMPRESSION_LEVEL) != Z_OK) {
if(compress2(compressBuffer.data(),
&compressBufferSize,
deviceFirmware.data(),
static_cast<decltype(compressBufferSize)>(deviceFirmware.size()),
COMPRESSION_LEVEL)
!= Z_OK) {
throw std::runtime_error("Error while compressing device firmware\n");
}

Expand Down Expand Up @@ -730,7 +735,7 @@ std::tuple<bool, std::string> DeviceBootloader::flashConfigData(nlohmann::json c
setConfigReq.offset = bootloader::getStructure(type).offset.at(bootloader::Section::BOOTLOADER_CONFIG);
}
setConfigReq.numPackets = 1;
setConfigReq.totalSize = bson.size();
setConfigReq.totalSize = static_cast<decltype(setConfigReq.totalSize)>(bson.size());
setConfigReq.clearConfig = 0;
if(!sendRequest(setConfigReq)) return {false, "Couldn't send request to flash configuration data"};

Expand Down Expand Up @@ -781,7 +786,7 @@ void DeviceBootloader::bootMemory(const std::vector<uint8_t>& embeddedFw) {
// Then wait for the link to fall down
try {
stream->read();
} catch(const std::exception& ex) {
} catch(const std::exception&) {
// ignore
}
}
Expand All @@ -795,7 +800,7 @@ void DeviceBootloader::bootUsbRomBootloader() {
// Then wait for the link to fall down
try {
stream->read();
} catch(const std::exception& ex) {
} catch(const std::exception&) {
// ignore
}
}
Expand Down Expand Up @@ -851,7 +856,7 @@ bool DeviceBootloader::sendRequest(const T& request) {

try {
stream->write((uint8_t*)&request, sizeof(T));
} catch(const std::exception& ex) {
} catch(const std::exception&) {
return false;
}

Expand Down Expand Up @@ -934,15 +939,15 @@ std::string DeviceBootloader::Config::getDnsAltIPv4() {
}

void DeviceBootloader::Config::setUsbTimeout(std::chrono::milliseconds ms) {
usb.timeoutMs = ms.count();
usb.timeoutMs = static_cast<decltype(usb.timeoutMs)>(ms.count());
}

std::chrono::milliseconds DeviceBootloader::Config::getUsbTimeout() {
return std::chrono::milliseconds(usb.timeoutMs);
}

void DeviceBootloader::Config::setNetworkTimeout(std::chrono::milliseconds ms) {
network.timeoutMs = ms.count();
network.timeoutMs = static_cast<decltype(network.timeoutMs)>(ms.count());
}

std::chrono::milliseconds DeviceBootloader::Config::getNetworkTimeout() {
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline/Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void PipelineImpl::setCameraTuningBlobPath(const std::string& path) {
auto asset = assetManager.set(assetKey, path);

globalProperties.cameraTuningBlobUri = asset->getRelativeUri();
globalProperties.cameraTuningBlobSize = asset->data.size();
globalProperties.cameraTuningBlobSize = static_cast<uint32_t>(asset->data.size());
}

void PipelineImpl::setXLinkChunkSize(int sizeBytes) {
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline/node/NeuralNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void NeuralNetwork::setBlobPath(const std::string& path) {

NeuralNetworkProperties& properties = getPropertiesRef();
properties.blobUri = asset->getRelativeUri();
properties.blobSize = asset->data.size();
properties.blobSize = static_cast<uint32_t>(asset->data.size());
}

void NeuralNetwork::setNumPoolFrames(int numFrames) {
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline/node/StereoDepth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void StereoDepth::loadMeshData(const std::vector<std::uint8_t>& dataLeft, const
assetKey = "meshRight";
properties.mesh.meshRightUri = assetManager.set(assetKey, meshAsset)->getRelativeUri();

properties.mesh.meshSize = meshAsset.data.size();
properties.mesh.meshSize = static_cast<uint32_t>(meshAsset.data.size());
}

void StereoDepth::loadMeshFiles(const std::string& pathLeft, const std::string& pathRight) {
Expand Down
22 changes: 11 additions & 11 deletions src/utility/matrixOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ std::vector<std::vector<float>> matMul(std::vector<std::vector<float>>& firstMat
* Function to get cofactor of A[p][q] in temp. n is current
* dimension of part of A that we are calculating cofactor for.
*/
static void getCofactor(std::vector<std::vector<float>>& A, std::vector<std::vector<float>>& temp, int p, int q, int n) {
int i = 0, j = 0;
static void getCofactor(std::vector<std::vector<float>>& A, std::vector<std::vector<float>>& temp, size_t p, size_t q, size_t n) {
size_t i = 0, j = 0;

// Looping for each element of the matrix
for(int row = 0; row < n; row++) {
for(int col = 0; col < n; col++) {
for(size_t row = 0; row < n; ++row) {
for(size_t col = 0; col < n; ++col) {
// Copying into temporary matrix only those element
// which are not in given row and column
if(row != p && col != q) {
Expand All @@ -58,7 +58,7 @@ static void getCofactor(std::vector<std::vector<float>>& A, std::vector<std::vec
// reset col index
if(j == n - 1) {
j = 0;
i++;
++i;
}
}
}
Expand All @@ -67,7 +67,7 @@ static void getCofactor(std::vector<std::vector<float>>& A, std::vector<std::vec

/* Recursive function for finding determinant of matrix.
n is current dimension of A[][]. */
static float determinant(std::vector<std::vector<float>>& A, int n) {
static float determinant(std::vector<std::vector<float>>& A, size_t n) {
float D = 0; // Initialize result

// Base case : if matrix contains single element
Expand All @@ -77,7 +77,7 @@ static float determinant(std::vector<std::vector<float>>& A, int n) {
int sign = 1; // To store sign multiplier

// Iterate for each element of first row
for(int f = 0; f < n; f++) {
for(size_t f = 0; f < n; ++f) {
// Getting Cofactor of A[0][f]
getCofactor(A, temp, 0, f, n);
D += sign * A[0][f] * determinant(temp, n - 1);
Expand All @@ -100,8 +100,8 @@ static void adjoint(std::vector<std::vector<float>>& A, std::vector<std::vector<
int sign = 1;
std::vector<std::vector<float>> temp(A.size(), std::vector<float>(A.size(), 0));

for(size_t i = 0; i < A.size(); i++) {
for(size_t j = 0; j < A.size(); j++) {
for(size_t i = 0; i < A.size(); ++i) {
for(size_t j = 0; j < A.size(); ++j) {
// Get cofactor of A[i][j]
getCofactor(A, temp, i, j, A.size());

Expand Down Expand Up @@ -136,8 +136,8 @@ bool matInv(std::vector<std::vector<float>>& A, std::vector<std::vector<float>>&

std::vector<float> temp;
// Find Inverse using formula "inverse(A) = adj(A)/det(A)"
for(size_t i = 0; i < A.size(); i++) {
for(size_t j = 0; j < A.size(); j++) {
for(size_t i = 0; i < A.size(); ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious on the i++ -> ++i changes, were there any warnings triggered by the previous version, or it's more like a style preference?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Porbably style, pre-increment is faster than post-increment, theoretically, but not for POD.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre is my preferred for best opportunity for optimization, see notes https://en.cppreference.com/w/cpp/language/operator_incdec
No guarantee compilers old/new on all platforms won't use the slow path on POD. So I prefer pre.

I did not do it everywhere i that matrix code as some places need the post behavior.

FYI, the Eigen library is fantastically excellent and all header. Even for light use like cali processing. They've been testing and optimizing across years and more platforms than most anyone could redo.

for(size_t j = 0; j < A.size(); ++j) {
temp.push_back(adj[i][j] / det);
}
inverse.push_back(temp);
Expand Down