This code demonstrates how to generate a grayscale sphere in a 3D volume.
This code demonstrates how to generate a grayscale sphere in a 3D volume.All voxels inside the sphere have value = 255, and others are 0.
#include <iostream>
#include <vector>
#include <cmath>
#include <iolink/view/ImageViewFactory.h>
std::shared_ptr<ImageView>
{
{
throw std::runtime_error("grayscaleSphere: shape must have 3 dimensions");
}
std::shared_ptr<ImageView> image = ImageViewFactory::allocate(shape, dt);
image->setAxesInterpretation(ImageTypeId::VOLUME);
image->setImageInterpretation(ImageInterpretation::GRAYSCALE);
size_t radius = std::min(center[0], center[1]);
radius = std::min(radius, center[2]);
uint8_t filledVoxel = 255;
uint8_t emptyVoxel = 0;
for (size_t z = 0; z < shape[2]; ++z)
{
for (size_t y = 0; y < shape[1]; ++y)
{
for (size_t x = 0; x < shape[0]; ++x)
{
VectorXi64 voxelPosition{
static_cast<int64_t
>(x),
static_cast<int64_t
>(y),
static_cast<int64_t
>(z)};
double distance = delta.
length();
if (distance <= static_cast<double>(radius))
{
image->write({x, y, z}, &filledVoxel);
}
else
{
image->write({x, y, z}, &emptyVoxel);
}
}
}
}
return image;
}
int
main(int argc, char** argv)
{
std::shared_ptr<ImageView> image = grayscaleSphere({100, 100, 100});
std::cout << "Created volume: " << image->toString() << std::endl;
return EXIT_SUCCESS;
}
Stores information about a data type.
Definition DataType.h:162
A dynamically sized arithmetic vector.
Definition VectorX.h:18
double length() const
Returns the vector's norm.
size_t size() const
Returns the size of the vector, which also correspond to vector number of dimension.
Definition VectorX.h:77
All IOLink symbols are enclosed in this namespace.
Definition ArrayX.h:8