Skip to content

Instantly share code, notes, and snippets.

@dgobbi
dgobbi / ImageStreaming.cxx
Last active November 9, 2015 20:32
An example of using a writer that automatically streams image data through VTK.
//
// A simple example of image streaming, using a reader that can stream
// from a file and a writer than can stream to a file.
//
// Currently, the only image writer in VTK that streams from its input
// is the vtkMINCImageWriter.
//
// Please send any comments to david.gobbi@gmail.com
//
#include <stdio.h>
#include <stdlib.h>
// This is a program for converting a binary image (little-endian)
// into a set of integers that indicate the position where the bit
// value changes. It is much faster than going through bit-by-bit.
int main(int argc, char *argv[])
{
static const int debruijn[32] =
!!ARBfp1.0
# This is a short fragment program for bicubic texture interpolation.
# It takes one parameter, a vector that gives the texture size as
# (xsize, ysize, 1.0/xsize, 1.0/ysize). The cubic interpolation weights
# are computed by the shader, so it is not necessary to supply an
# interpolation kernel texture.
# width, height, xspacing, yspacing
"""
This is a simple class that can catch errors from VTK objects.
"""
import vtk
class ErrorObserver:
def __init__(self):
self.__ErrorOccurred = False
// Clip a line segment defined by endpoints at p1[3], p2[3] by a
// collection of n planes defined by the equation ax+by+cz+d=0.
// The parametric coordinates t1,t2 of new endpoints are returned,
// as well as the indices i1,i2 of the entry and exit planes for the line.
// Result values of -1 for i1 or i2 signify that the line was not clipped
// at the p1 end or the p2 end respectively. The function return value
// will be 0 if the whole line segment lies outside, or 1 if the line
// segment lies fully or partially inside the planes.
int clipLineWithPlanes(const double p1[3], const double p2[3],
@dgobbi
dgobbi / bracefix.py
Created July 3, 2016 19:31
Modernize VTK brace style, optionally convert to my own K&R-based style
#! /usr/bin/env python
"""
Usage: python bracefix.py [--test] <file1> [<file2> ...]
This script takes old-style "Whitesmiths" indented VTK source files as
input, and re-indents the braces according to the new VTK style.
Only the brace indentation is modified.
If called with the --test option, then it will print an error message
for each file that it would modify, but it will not actually modify the
import numpy as np
def ellipsoid(radii, dtype='d'):
"""Generate an ellipsoid with the specified radii.
The ellipsoid will be centered in the output, which will always
have odd dimensions. The surface of the sphere will be antialiased,
that is, a linear ramp is applied at the surface instead of a hard
edge. The size of the ramp is exactly one sample spacing, i.e. points
right on the surface will have a value of 0.5 while points that are
import numpy
Kb = 0.114
Kr = 0.299
full = numpy.array([
[1.0, 0.0, 2.0*(1.0-Kr) ],
[1.0, -2.0*Kb*(1.0-Kb)/(1.0-Kr-Kb), -2.0*Kr*(1.0-Kr)/(1.0-Kr-Kb) ],
[1.0, 2.0*(1.0-Kb), 0.0 ]
])
@dgobbi
dgobbi / nifti_readwrite.py
Created September 16, 2019 16:30
Example of reading and writing a NIfTI file with nibabel
"""
Program nifti_readwrite.py
Sep 16, 2019
David Gobbi
"""
import sys
import argparse
import nibabel as nib
@dgobbi
dgobbi / readdump.py
Last active August 7, 2020 17:29
Parse the output from dicomdump and write out as json
#! /usr/bin/env python
"""
This program reads the output from "dicomdump" and converts it to json.
The latest version of this code can be found at gist.github.com/dgobbi
Note that this code is incomplete, incorrect, and may destroy your data.
It comes with absolutely no warranties. Use at your own risk.
"""