package de.mpi.rgblab.utils;
import java.awt.Toolkit;
import java.util.Iterator;
import java.util.Vector;
import de.mpi.rgblab.color.ColorModel;
import de.mpi.rgblab.color.ColorTransformer;
public class VisUtil {
private static int LIGHT = 100;
public static void rgbToLabColors(Vector<ColorModel> rgbColors,
Vector<ColorModel> labColors) {
// copy the the vector to an array
ColorModel[] my_rgbColor = new ColorModel[rgbColors.size()];
rgbColors.copyInto(my_rgbColor);
// transform to lab space
ColorModel labColor[] = ColorTransformer.rgb2lab(my_rgbColor);
// delete the old elements
if (false == labColors.isEmpty())
labColors.removeAllElements();
// array to vector
for (int i = 0; i < labColor.length; i++) {
labColors.addElement(labColor[i]);
}
}
public static void labToRGBColors(Vector<ColorModel> rgbColors,
Vector<ColorModel> labColors) {
// copy the the vector to an array
ColorModel[] my_labColor = new ColorModel[labColors.size()];
labColors.copyInto(my_labColor);
// transform to rgb space
ColorModel rgbColor[] = ColorTransformer.lab2rgb(my_labColor);
//check boundaries
for (int i =0; i < rgbColor.length; i++) {
rgbColor[i].setDimension_1(RGB.getCorrectR(rgbColor[i].getDimension_1()));
rgbColor[i].setDimension_2(RGB.getCorrectG(rgbColor[i].getDimension_2()));
rgbColor[i].setDimension_3(RGB.getCorrectB(rgbColor[i].getDimension_3()));
}
// delete the old elements
if (false == rgbColors.isEmpty())
rgbColors.removeAllElements();
// array to vector
for (int i = 0; i < rgbColor.length; i++) {
rgbColors.addElement(rgbColor[i]);
}
}
public static double eucl_distance_center_surrounding(ColorModel center,
Vector<ColorModel> colors, int[] number_of_elements) {
Iterator it = colors.iterator();
double l = 0;
double b = 0;
double a = 0;
int k = 0;
int area = 0;
ColorModel my_color;
while (it.hasNext()) {
my_color = (ColorModel) it.next();
if (number_of_elements[k] > 0) {
area += number_of_elements[k];
l += my_color.getDimension_1() * number_of_elements[k];
a += my_color.getDimension_2() * number_of_elements[k];
b += my_color.getDimension_3() * number_of_elements[k];
}
k++;
}
// white color
l += LIGHT * number_of_elements[k];
area += number_of_elements[k];
// b += 0 * ...
// a += 0 * ...
l /= area;
b /= area;
a /= area;
l -= center.getDimension_1();
a -= center.getDimension_2();
b -= center.getDimension_3();
return Math.sqrt(l * l + a * a + b * b);
}
public static double eucl_distance_center_surrounding(ColorModel center,
Vector<ColorModel> colors, Vector<Integer> number_of_elements, int classNumbers) {
Iterator it = colors.iterator();
double l = 0;
double b = 0;
double a = 0;
int k = 0;
int area = 0;
ColorModel my_color;
//for (int i =0; i < classNumbers; i++) {
while (k < classNumbers) {
my_color = (ColorModel) it.next();
if (number_of_elements.get(k) > 0) {
area += number_of_elements.get(k);
l += my_color.getDimension_1() * number_of_elements.get(k);
a += my_color.getDimension_2() * number_of_elements.get(k);
b += my_color.getDimension_3() * number_of_elements.get(k);
}
k++;
}
// white color
l += LIGHT * number_of_elements.get(k);
area += number_of_elements.get(k);
// b += 0 * ...
// a += 0 * ...
l /= area;
b /= area;
a /= area;
l -= center.getDimension_1();
a -= center.getDimension_2();
b -= center.getDimension_3();
return Math.sqrt(l * l + a * a + b * b);
}
public static float eucl_distance_center_surrounding(ColorModel center,
Vector<ColorModel> colors, short[][] number_of_elements, int point, int classNumbers) {
Iterator it = colors.iterator();
float l = 0;
float b = 0;
float a = 0;
int k = 0;
int area = 0;
ColorModel my_color;
//for (int i =0; i < classNumbers; i++) {
while (k < classNumbers) {
my_color = (ColorModel) it.next();
if (number_of_elements[k][point] > 0) {
area += number_of_elements[k][point];
l += my_color.getDimension_1() * number_of_elements[k][point];
a += my_color.getDimension_2() * number_of_elements[k][point];
b += my_color.getDimension_3() * number_of_elements[k][point];
}
k++;
}
// white color
l += LIGHT * number_of_elements[k][point];
area += number_of_elements[k][point];
// b += 0 * ...
// a += 0 * ...
l /= area;
b /= area;
a /= area;
l -= (float)center.getDimension_1();
a -= (float)center.getDimension_2();
b -= (float)center.getDimension_3();
return (float)Math.sqrt(l * l + a * a + b * b);
}
public static Vector<Double> eucl_distance_center_surrounding_as_vector(
ColorModel center, Vector<Double> surrounding_color) {
surrounding_color.set(0, surrounding_color.get(0) - center.getDimension_1());
surrounding_color.set(1, surrounding_color.get(1) - center.getDimension_2());
surrounding_color.set(2, surrounding_color.get(2) - center.getDimension_3());
return surrounding_color;
}
public static Vector<Double> mean_surrounding_color_as_vector(
ColorModel center, Vector<ColorModel> colors,
Vector<Integer> number_of_elements, int classNumbers) {
Iterator it = colors.iterator();
double l = 0;
double b = 0;
double a = 0;
int k = 0;
int area = 0;
ColorModel my_color;
//while (it.hasNext()) {
while (k < classNumbers) {
my_color = (ColorModel) it.next();
if (number_of_elements.get(k) > 0) {
area += number_of_elements.get(k);
l += my_color.getDimension_1() * number_of_elements.get(k);
a += my_color.getDimension_2() * number_of_elements.get(k);
b += my_color.getDimension_3() * number_of_elements.get(k);
}
k++;
}
// white color
l += LIGHT * number_of_elements.get(k);
area += number_of_elements.get(k);
// b += 0 * ...
// a += 0 * ...
l /= area;
b /= area;
a /= area;
Vector<Double> ret = new Vector();
ret.add(l);
ret.add(a);
ret.add(b);
return ret;
// return Math.sqrt(l * l + a * a + b * b);
}
public static int getViewRadius(int distance) {
return getRadius(distance, 2);
}
public static int getRadius(int distance, double viewingAngle) {
int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
double viewingAngleRadians = Math.toRadians(viewingAngle / 2);
// calculating the radius in inches
double radius = distance * Math.tan(viewingAngleRadians);
// calculating the radius in pixels
radius *= dpi;
return (int) radius;
}
/**
* calculate the the positive quarter (first quandrant) for the circle. It
* returns the boundaries for the slice used for applying the integral image
* algorithm.
*
*
* the center of the circle is the 0, 0
*
* example if the radius is 30
*
* @param radius
* @return
*/
public static int[] getSliceBoundaries(int radius) {
int diameter = radius * 8;
int[] circle = new int[radius + 1];
int center = radius;
// circle[0] = 0;
// circle[0][1] = 1;
double theta;
int x, y;
int last_x = radius;
circle[0] = radius;
int last_y = 0;
for (int i = 0; i <= diameter; i++) {
theta = Math.PI / 2 * i / (diameter);
x = (int) (float) Math.round((radius * Math.cos(theta) + 0));
y = (int) (float) Math.round((radius * Math.sin(theta) + 0));
if (last_x != x) {
// System.out.print(last_x);
// System.out.print(" ");
// System.out.println(last_y);
circle[last_x] = last_y;
}
last_x = x;
last_y = y;
}
// circle[diameter+1] = 0;
// circle[diameter+1][1] = 0;
return circle;
}
}