use of com.forstudy.efjava.ch03.item10.Point in project java-monitoring by googleapis.
the class QuickstartSample method quickstart.
public static void quickstart(String projectId) throws IOException {
// once, and can be reused for multiple requests.
try (MetricServiceClient metricServiceClient = MetricServiceClient.create()) {
// Prepares an individual data point
TimeInterval interval = TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
TypedValue value = TypedValue.newBuilder().setDoubleValue(123.45).build();
Point point = Point.newBuilder().setInterval(interval).setValue(value).build();
List<Point> pointList = new ArrayList<>();
pointList.add(point);
ProjectName name = ProjectName.of(projectId);
// Prepares the metric descriptor
Map<String, String> metricLabels = new HashMap<String, String>();
metricLabels.put("store_id", "Pittsburg");
Metric metric = Metric.newBuilder().setType("custom.googleapis.com/stores/daily_sales").putAllLabels(metricLabels).build();
// Prepares the monitored resource descriptor
Map<String, String> resourceLabels = new HashMap<String, String>();
resourceLabels.put("project_id", projectId);
MonitoredResource resource = MonitoredResource.newBuilder().setType("global").putAllLabels(resourceLabels).build();
// Prepares the time series request
TimeSeries timeSeries = TimeSeries.newBuilder().setMetric(metric).setResource(resource).addAllPoints(pointList).build();
List<TimeSeries> timeSeriesList = new ArrayList<>();
timeSeriesList.add(timeSeries);
CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(name.toString()).addAllTimeSeries(timeSeriesList).build();
// Writes time series data
metricServiceClient.createTimeSeries(request);
System.out.printf("Done writing time series data.%n");
}
}
use of com.forstudy.efjava.ch03.item10.Point in project multiview-simulation by PreibischLab.
the class SimulateMultiViewAberrations method projectToCamera.
public static Img<FloatType> projectToCamera(// final RandomAccessibleInterval< FloatType > imgIn,
final RandomAccessibleInterval<FloatType> imgRi, final RandomAccessibleInterval<FloatType> refr, final double ri, final int currentzPlane) {
// the refracted image
final Img<FloatType> proj = new ArrayImgFactory<FloatType>(new FloatType()).create(new long[] { refr.dimension(0), refr.dimension(1) });
final Img<FloatType> tmp = new ArrayImgFactory<FloatType>(new FloatType()).create(imgRi);
final RandomAccess<FloatType> rt = Views.extendZero(tmp).randomAccess();
// final RealRandomAccess< FloatType > rrIm = Views.interpolate( Views.extendMirrorSingle( imgIn ), new NLinearInterpolatorFactory<>() ).realRandomAccess();
final RealRandomAccess<FloatType> rrRi = Views.interpolate(Views.extendMirrorSingle(imgRi), new NLinearInterpolatorFactory<>()).realRandomAccess();
final RealRandomAccess<FloatType> rrRef = Views.interpolate(Views.extendMirrorSingle(refr), new NLinearInterpolatorFactory<>()).realRandomAccess();
// row, column
final double[][] matrix = new double[3][3];
final double[] eigenVector = new double[3];
// the incoming direction of the light
final double[] rayVector = new double[3];
// the outgoing, refracted direction of the light
final double[] refractedRay = new double[3];
// the current position of the ray
final double[] rayPosition = new double[3];
// air (intensity == 0)
final double nA = 1.00;
// ri // water (intensity == 1)
final double nB = 1.01;
final Cursor<FloatType> c = proj.localizingCursor();
final double sigma = 4.0;
final double two_sq_sigma = 2 * sigma * sigma;
while (c.hasNext()) {
c.fwd();
final boolean debug = false;
// if ( c.getIntPosition( 0 ) == 78 && c.getIntPosition( 1 ) == 75 )
// debug = true;
double avgValue = 0;
for (int i = 0; i < 500; ++i) {
rayPosition[0] = c.getIntPosition(0) + (rnd.nextDouble() - 0.5);
rayPosition[1] = c.getIntPosition(1) + (rnd.nextDouble() - 0.5);
rayPosition[2] = 1;
// (rnd.nextDouble() - 0.5)/100;
rayVector[0] = 0;
// (rnd.nextDouble() - 0.5)/100;
rayVector[1] = 0;
rayVector[2] = 1;
double signal = 0;
long maxMoves = imgRi.dimension(2);
int moves = 0;
while (inside(rayPosition, refr) && moves < maxMoves) {
++moves;
rrRi.setPosition(rayPosition);
// normal vector of refraction plane (still maybe needs to be inverted to point towards the incoming signal)
Hessian.computeHessianMatrix3D(rrRi, matrix);
double ev = Hessian.computeLargestEigenVectorAndValue3d(matrix, eigenVector);
if (Math.abs(ev) > 0.01) {
// compute refractive index change
rrRi.setPosition(rayPosition);
rrRi.move(-rayVector[0], 0);
rrRi.move(-rayVector[1], 1);
rrRi.move(-rayVector[2], 2);
// intensity at the origin of the ray
final double i0 = rrRi.get().get();
rrRi.move(2 * rayVector[0], 0);
rrRi.move(2 * rayVector[1], 1);
rrRi.move(2 * rayVector[2], 2);
// intensity at the projected location of the ray
final double i1 = rrRi.get().get();
final double n0 = (nB - nA) * i0 + nA;
final double n1 = (nB - nA) * i1 + nA;
// if ( debug )
// System.out.println( n0 + " " + n1 );
final double thetaI = Raytrace.incidentAngle(rayVector, eigenVector);
final double thetaT = Raytrace.refract(rayVector, eigenVector, n0, n1, thetaI, refractedRay);
// total reflection
if (Double.isNaN(thetaT)) {
if (debug)
System.out.println("reflect: " + thetaI + " " + thetaT);
// Raytrace.reflect( rayVector, eigenVector, refractedRay );
refractedRay[0] = rayVector[0];
refractedRay[1] = rayVector[1];
refractedRay[2] = rayVector[2];
}
Raytrace.norm(refractedRay);
// update the ray vector
rayVector[0] = refractedRay[0];
rayVector[1] = refractedRay[1];
rayVector[2] = refractedRay[2];
if (debug)
System.out.println(Util.printCoordinates(rayVector));
}
// place a gaussian sphere
rrRef.setPosition(rayPosition);
final double zOffset = Math.abs(rayPosition[2] - currentzPlane);
/*
* 0 > 1
* 0.5 > 1.5
* 1 > 2
*/
// signal += ( rrRef.get().get() / Math.pow( zOffset + 1.0, 1.00 / 1.75 ));
signal += rrRef.get().get() * getGaussValue(zOffset, two_sq_sigma);
rayPosition[0] += rayVector[0];
rayPosition[1] += rayVector[1];
rayPosition[2] += rayVector[2];
if (debug) {
rt.setPosition(Math.round(rayPosition[0]), 0);
rt.setPosition(Math.round(rayPosition[1]), 1);
rt.setPosition(Math.round(rayPosition[2]), 2);
rt.get().set(rt.get().get() + 1.0f);
// System.out.println( "signal: " + signal + " @ " + Util.printCoordinates( rayPosition ) );
}
}
avgValue += signal;
}
c.get().set((float) (avgValue / 10.0));
if (debug)
ImageJFunctions.show(tmp);
}
return proj;
}
use of com.forstudy.efjava.ch03.item10.Point in project multiview-simulation by PreibischLab.
the class SimulateMultiViewAberrations method refract3d.
public static VolumeInjection refract3d(final RandomAccessibleInterval<FloatType> imgIn, final RandomAccessibleInterval<FloatType> imgRi, final boolean illum, final int z, final double lsMiddle, final double lsEdge, final double ri) {
// the refracted image
final Img<FloatType> img = new ArrayImgFactory<FloatType>(new FloatType()).create(imgIn);
final Img<FloatType> weight = new ArrayImgFactory<FloatType>(new FloatType()).create(imgIn);
// for drawing individual rays ...
// final RandomAccess< FloatType > rOut = img.randomAccess();
// final long[] intPos = new long[ 3 ];
final RealRandomAccess<FloatType> rrIm = Views.interpolate(Views.extendMirrorSingle(imgIn), new NLinearInterpolatorFactory<>()).realRandomAccess();
final RealRandomAccess<FloatType> rrRi = Views.interpolate(Views.extendMirrorSingle(imgRi), new NLinearInterpolatorFactory<>()).realRandomAccess();
// row, column
final double[][] matrix = new double[3][3];
final double[] eigenVector = new double[3];
final double[] sigma = new double[] { 0.5, 0.5, 0.5 };
final VolumeInjection inject = new VolumeInjection(img, weight, sigma);
// the incoming direction of the light
final double[] rayVector = new double[3];
// the outgoing, refracted direction of the light
final double[] refractedRay = new double[3];
// the current position of the ray
final double[] rayPosition = new double[3];
// air (intensity == 0)
final double nA = 1.00;
// water (intensity == 1)
final double nB = ri;
System.out.println("middle: " + lsMiddle);
System.out.println("lsEdge: " + lsEdge);
final Lightsheet ls = new Lightsheet(img.dimension(0) / 2.0, lsMiddle, img.dimension(0), lsEdge);
System.out.println("starting...");
long time = System.currentTimeMillis();
// for each point on the xz plane at y=0
final int numRays = 200000;
final long maxMoves = img.dimension(2);
final Random rnd = new Random(2423);
for (int i = 0; i < numRays; ++i) {
// x;//c.getIntPosition( 0 );
rayPosition[0] = rnd.nextDouble() * imgIn.max(0);
rayPosition[1] = illum ? (int) imgIn.dimension(1) - 1 : 0;
final double lightsheetthickness = ls.predict(rayPosition[0]);
// c.getIntPosition( 1 );
rayPosition[2] = z + (rnd.nextDouble() * lightsheetthickness) - lightsheetthickness / 2.0;
rayVector[0] = (rnd.nextDouble() - 0.5) / 5;
rayVector[1] = illum ? -1 : 1;
rayVector[2] = 0;
Raytrace.norm(rayVector);
int moves = 0;
while (inside(rayPosition, imgIn) && moves < maxMoves) {
++moves;
rrIm.setPosition(rayPosition);
rrRi.setPosition(rayPosition);
final float valueIm = rrIm.get().get();
// normal vector of refraction plane (still maybe needs to be inverted to point towards the incoming signal)
Hessian.computeHessianMatrix3D(rrRi, matrix);
double ev = Hessian.computeLargestEigenVectorAndValue3d(matrix, eigenVector);
if (Math.abs(ev) > 0.01) {
// compute refractive index change
rrRi.setPosition(rayPosition);
rrRi.move(-rayVector[0], 0);
rrRi.move(-rayVector[1], 1);
rrRi.move(-rayVector[2], 2);
// intensity at the origin of the ray
final double i0 = rrRi.get().get();
rrRi.move(2 * rayVector[0], 0);
rrRi.move(2 * rayVector[1], 1);
rrRi.move(2 * rayVector[2], 2);
// intensity at the projected location of the ray
final double i1 = rrRi.get().get();
final double n0 = (nB - nA) * i0 + nA;
final double n1 = (nB - nA) * i1 + nA;
final double thetaI = Raytrace.incidentAngle(rayVector, eigenVector);
final double thetaT = Raytrace.refract(rayVector, eigenVector, n0, n1, thetaI, refractedRay);
// total reflection
if (Double.isNaN(thetaT)) {
refractedRay[0] = rayVector[0];
refractedRay[1] = rayVector[1];
refractedRay[2] = rayVector[2];
// continue; // that's an expensive getting stuck
// Raytrace.reflect( rayVector, eigenVector, refractedRay );
}
Raytrace.norm(refractedRay);
// update the ray vector
rayVector[0] = refractedRay[0];
rayVector[1] = refractedRay[1];
rayVector[2] = refractedRay[2];
}
// place a gaussian sphere
inject.addNormalizedGaussian(valueIm, rayPosition);
/*
// for drawing individual rays ...
intPos[ 0 ] = Math.round( rayPosition[ 0 ] );
intPos[ 1 ] = Math.round( rayPosition[ 1 ] );
intPos[ 2 ] = Math.round( rayPosition[ 2 ] );
rOut.setPosition( intPos );
rOut.get().set( value );
*/
rayPosition[0] += rayVector[0];
rayPosition[1] += rayVector[1];
rayPosition[2] += rayVector[2];
}
}
System.out.println(" ... " + (System.currentTimeMillis() - time));
return inject;
}
use of com.forstudy.efjava.ch03.item10.Point in project multiview-simulation by PreibischLab.
the class SimulateMultiViewAberrations method multiSpheres.
public static <T extends RealType<T>> void multiSpheres(final RandomAccessibleInterval<T> image, final RandomAccessibleInterval<T> ri, final int scale, final Random rnd) {
// the number of dimensions
final int numDimensions = image.numDimensions();
// define the center and radius
final Point center1 = new Point(image.numDimensions());
final Point center2 = new Point(image.numDimensions());
final Point center3 = new Point(image.numDimensions());
long minSize = image.dimension(0);
for (int d = 0; d < numDimensions; ++d) {
long size = image.dimension(d);
center1.setPosition(size / 2, d);
center2.setPosition(size / 2, d);
center3.setPosition(size / 2, d);
minSize = Math.min(minSize, size);
}
// define the maximal radius of the small spheres
int maxRadius = 10 * scale;
// compute the radius of the large sphere so that we do not draw
// outside of the defined interval
// -45;
long radiusLargeSphere1 = minSize / 2 - 47 * scale - 1;
final ArrayList<Pair<Pair<Point, Long>, double[]>> centers = new ArrayList<>();
// defines a small sphere (location, radius), [minValueIm, maxValueIm, minValueRi, maxValueRi]
centers.add(new ValuePair<>(new ValuePair<>(center2, radiusLargeSphere1), new double[] { 0.5, 1.0, 1.0, 1.1 }));
for (final Pair<Pair<Point, Long>, double[]> center : centers) {
final double minValueIm = center.getB()[0];
final double maxValueIm = center.getB()[1];
final double minValueRi = center.getB()[2];
final double maxValueRi = center.getB()[3];
// define a hypersphere (n-dimensional sphere)
final HyperSphere<T> hyperSphereIm = new HyperSphere<T>(image, center.getA().getA(), center.getA().getB());
final HyperSphere<T> hyperSphereRi = new HyperSphere<T>(ri, center.getA().getA(), center.getA().getB());
// create a cursor on the hypersphere
final HyperSphereCursor<T> cursorIm = hyperSphereIm.cursor();
final HyperSphereCursor<T> cursorRi = hyperSphereRi.cursor();
final int size = (int) hyperSphereIm.size();
IJ.showProgress(0.0);
int i = 0;
while (cursorIm.hasNext()) {
cursorIm.fwd();
cursorRi.fwd();
if (maxValueIm == minValueIm)
cursorIm.get().setReal(minValueIm);
if (maxValueRi == minValueRi)
cursorRi.get().setReal(minValueRi);
// the random radius of the current small hypersphere
int radius = Math.max(rnd.nextInt(maxRadius) + 1, maxRadius - 1);
// instantiate a small hypersphere at the location of the current pixel
// in the large hypersphere
final HyperSphere<T> smallSphereIm = new HyperSphere<T>(image, cursorIm, radius);
final HyperSphere<T> smallSphereRi = new HyperSphere<T>(ri, cursorRi, radius);
// define the random intensity for this small sphere
double randomValue = rnd.nextDouble();
// take only every 4^dimension'th pixel by chance so that it is not too crowded
if ((randomValue * 100000) < 1) {
// scale to right range
randomValue = rnd.nextDouble();
double randomValueRi = randomValue * (maxValueRi - minValueRi) + minValueRi;
double randomValueIm = randomValue * (maxValueIm - minValueIm) + minValueIm;
// brighter than the existing one
if (maxValueIm != minValueIm)
for (final T value : smallSphereIm) value.setReal(Math.max(randomValueIm, value.getRealDouble()));
if (maxValueRi != minValueRi) {
for (final T value : smallSphereRi) {
if (value.getRealDouble() == 5.0)
value.setReal(randomValueRi);
else
value.setReal(Math.max(randomValueRi, value.getRealDouble()));
}
}
}
IJ.showProgress(++i, size);
}
}
}
use of com.forstudy.efjava.ch03.item10.Point in project study-effective-java by doyoung0205.
the class Item10Test method transitivity.
@Test
@DisplayName("equals 추이성 위배")
void transitivity() {
// given
Point p1 = new Point(1, 2);
ColorPoint p2 = new ColorPoint(1, 2, Color.ORANGE);
ColorPoint p3 = new ColorPoint(1, 2, Color.GREEN);
// when // then
assertTrue(p1.equals(p2));
assertTrue(p1.equals(p3));
assertFalse(p2.equals(p3));
}
Aggregations