// File: /workspace/github_cpp_makefile_star2_O1/jingdao[P]PointCloudApp/descriptor.cpp
// Function: Descriptor::kMeansClustering(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>*)
void Descriptor::kMeansClustering(std::vector<std::vector<int>> *indices) {
	int k = 10;
//	int k = (int) sqrt(numPoints / 2);
	//initialize
	for (int i=0;i<k;i++) {
		std::vector<int> v;
		indices->push_back(v);
	}
	int* associations = new int[numPoints]();
	float* means = new float[k * dimension];
	int iterations = 0;

	//choose initial means
	for (int i=0;i<k;i++) {
		int index = rand() % numPoints;
		memcpy(means + i * dimension, data + index * dimension, dimension * sizeof(float));
	}

	bool update = true;
	while (update) {
		update = false;
		for (int i=0;i<k;i++)
			(*indices)[i].clear();

		//assignment step
		for (int i=0;i<numPoints;i++) {
			//find nearest mean
			int minIndex;
			float minDistance;
			for (int j=0;j<k;j++) {
				float distance = 0;
				for (int l=0;l<dimension;l++)
					distance += (data[i*dimension+l] - means[j*dimension+l]) * (data[i*dimension+l] - means[j*dimension+l]);
				if (j==0 || distance < minDistance) {
					minIndex = j;
					minDistance = distance;
				}
			}
			if (minIndex != associations[i])
				update = true;
			associations[i] = minIndex;
			(*indices)[minIndex].push_back(i);
		}

		//update step
		for (int i=0;i<k;i++) {
			for (int j=0;j<dimension;j++) {
				means[i * dimension + j] = 0;
				size_t clusterSize = (*indices)[i].size();
				for (size_t l=0;l<clusterSize;l++) {
					means[i * dimension + j] += data[(*indices)[i][l] * dimension + j];
				}
				means[i * dimension + j] /= clusterSize;
			}
		}
		iterations++;
	}

//	printf("K-Means: Found %lu clusters (%d iterations)\n",indices->size(),iterations);

	delete[] associations;
	delete[] means;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl $0xa, %ebp
leaq 0x10(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7c10
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9827
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
decl %ebp
jne 0x97f4
movslq (%r14), %rax
leaq (,%rax,4), %r12
testq %rax, %rax
movq $-0x1, %r13
cmovsq %r13, %r12
movq %r12, %rdi
callq 0x2040
movq %rax, %r15
xorl %ebp, %ebp
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2100
movslq 0x4(%r14), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
leaq (%rcx,%rcx,4), %rdi
cmovsq %r13, %rdi
callq 0x2040
movq %rax, %r12
callq 0x2080
cltd
idivl (%r14)
movslq 0x4(%r14), %rax
movl %eax, %ecx
imull %ebp, %ecx
movslq %ecx, %rcx
leaq (%r12,%rcx,4), %rdi
imull %eax, %edx
movslq %edx, %rsi
shlq $0x2, %rsi
addq 0x8(%r14), %rsi
shlq $0x2, %rax
movq %rax, %rdx
callq 0x2180
incl %ebp
cmpl $0xa, %ebp
jne 0x987d
movl $0x1, %r11d
movq %rbx, 0x30(%rsp)
movq (%rbx), %rax
movl $0x8, %ecx
movq -0x8(%rax,%rcx), %rdx
cmpq %rdx, (%rax,%rcx)
je 0x98d9
movq %rdx, (%rax,%rcx)
addq $0x18, %rcx
cmpq $0xf8, %rcx
jne 0x98ca
movl $0x0, 0x10(%rsp)
xorl %eax, %eax
cmpl $0x0, (%r14)
jle 0x99d7
xorl %ebp, %ebp
movl 0x4(%r14), %ecx
movl %ecx, %edx
imull %eax, %edx
movslq %edx, %rdx
movslq %ecx, %rsi
shlq $0x2, %rdx
addq 0x8(%r14), %rdx
shlq $0x2, %rsi
movq %r12, %rdi
xorl %r8d, %r8d
movaps %xmm2, %xmm0
xorps %xmm2, %xmm2
testl %ecx, %ecx
jle 0x9946
xorl %r9d, %r9d
movss (%rdx,%r9,4), %xmm1
subss (%rdi,%r9,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm2
incq %r9
cmpq %r9, %rcx
jne 0x992a
testq %r8, %r8
sete %r9b
ucomiss %xmm2, %xmm0
seta %r10b
orb %r9b, %r10b
cmovnel %r8d, %r13d
jne 0x9960
movaps %xmm0, %xmm2
incq %r8
addq %rsi, %rdi
movaps %xmm2, %xmm0
cmpq $0xa, %r8
jne 0x9920
movslq %eax, %rcx
cmpl (%r15,%rcx,4), %r13d
movzbl %bpl, %ebp
cmovnel %r11d, %ebp
movl %r13d, (%r15,%rcx,4)
movslq %r13d, %rcx
movq (%rbx), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x99a6
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x99c2
leaq 0x10(%rsp), %rdx
movss %xmm2, 0xc(%rsp)
callq 0x80de
movss 0xc(%rsp), %xmm2
movl $0x1, %r11d
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
cmpl (%r14), %eax
jl 0x98fc
movl %ebp, %eax
movl 0x4(%r14), %ecx
movq 0x8(%r14), %rdx
xorl %esi, %esi
testl %ecx, %ecx
jle 0x9a92
movq 0x30(%rsp), %rdi
movq (%rdi), %r8
leaq (%rsi,%rsi,2), %r9
movq (%r8,%r9,8), %rdi
movq 0x8(%r8,%r9,8), %r8
movq %r8, %r9
subq %rdi, %r9
sarq $0x2, %r9
js 0x9a14
xorps %xmm0, %xmm0
cvtsi2ss %r9, %xmm0
jmp 0x9a30
movq %r9, %r10
shrq %r10
movl %r9d, %r11d
andl $0x1, %r11d
orq %r10, %r11
xorps %xmm0, %xmm0
cvtsi2ss %r11, %xmm0
addss %xmm0, %xmm0
movq %rsi, %r10
imulq %rcx, %r10
cmpq $0x1, %r9
adcq $0x0, %r9
leaq (%r12,%r10,4), %r10
xorl %r11d, %r11d
movl $0x0, (%r10,%r11,4)
cmpq %rdi, %r8
je 0x9a7a
movss (%r10,%r11,4), %xmm1
xorl %ebp, %ebp
movl (%rdi,%rbp,4), %ebx
imull %ecx, %ebx
addl %r11d, %ebx
movslq %ebx, %rbx
addss (%rdx,%rbx,4), %xmm1
movss %xmm1, (%r10,%r11,4)
incq %rbp
cmpq %rbp, %r9
jne 0x9a5b
movss (%r10,%r11,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%r10,%r11,4)
incq %r11
cmpq %rcx, %r11
jne 0x9a46
incq %rsi
cmpq $0xa, %rsi
jne 0x99e1
testb $0x1, %al
movq 0x30(%rsp), %rbx
movl $0x1, %r11d
jne 0x98c2
movq %r15, %rdi
callq 0x2260
movq %r12, %rdi
callq 0x2260
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9aeb
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x23a0
nop